Introduction :
Hi, In this tutorial you will look how to insert the data into database. using MVC. you can follow the below steps for this tutorial
Step 1 : Select MVC web application
Step 2 : Select Empty template with Razor view Engine.
Step 3 : Create the Database,Table for Which you are going to insert
Step 4 : Right Click on model folder add a class file to it.
Step 5 : Give the name to the class file created in Model folder.
Step 6 : Start writing code to model.
- Create Property class for the details to insert.
- Create function for inserting details( DB Layer Coding )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
namespace MVC_1_InsertData.Models
{
public class RegistrationDetails
{
//creatinng
property class for the details to insert
public string Name { get; set; }
public string EmailID { get; set; }
public string Mobilenumber { get; set; }
public string Address { get; set; }
SqlConnection con = new SqlConnection("Your connection
string");
SqlCommand cmd = new SqlCommand();
//Creating
function to insert details
public string InsertRegDetails(RegistrationDetails obj)
{
cmd.CommandText
= "Insert into [Table] values('" + obj.Name + "','" + obj.EmailID + "'," + Convert.ToInt64(obj.Mobilenumber) + ",'" +
obj.Address + "')";
cmd.Connection
= con;
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "Success";
}
catch (Exception es)
{
throw es;
}
}
}
}
Step 7 : Right Click on Controller folder and create new Controller.
Step 8: Give name to the controller followed by the text 'Controller'
Step 9 : Your basic controller Structure
Step 9 : Write coding to your Controller
Your controller code looks as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_1_InsertData.Models;
namespace MVC_1_InsertData.Controllers
{
public class RegController : Controller
{
//
Calling when we first hit controller.
public ActionResult
InsertDetails()
{
return View();
}
//
Calling on http post (on Submit)
[HttpPost]
public ActionResult InsertDetails(RegistrationDetails obj)
{
RegistrationDetails objreg = new RegistrationDetails();
string result = objreg.InsertRegDetails(obj);
ViewData["result"] = result;
ModelState.Clear();
return View();
}
}
}
Step 10 : Create view by placing your cursor near your ActionResult name as below.
Step 11 : Keep View name as it looks. check strongly typed view select your model.
Step 12 : Your View Firstly looks like below.
Step 13 : Add the below code to the view.
@model
MVC_1_InsertData.Models.RegistrationDetails
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>InsertDetails</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<table>
<tr>
<td>Name</td>
<td>@Html.TextBoxFor(x =>
x.Name)</td>
</tr>
<tr>
<td>Email ID</td>
<td>@Html.TextBoxFor(x =>
x.EmailID)</td>
</tr>
<tr>
<td>Mobile Number</td>
<td>@Html.TextBoxFor(x =>
x.Mobilenumber)</td>
</tr>
<tr>
<td>Address</td>
<td>@Html.TextAreaFor(x =>
x.Address)</td>
</tr>
<tr>
<td></td>
<td><input id="Submit" type="submit" value="submit" /></td>
</tr>
</table>
}
@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
ViewData["result"] = null;
<script type="text/javascript" language="javascript">
alert("Details saved Successfully");
</script>
}
}
</div>
</body>
</html>
Your Coding part is completed. Now Run your Application,you will find an error that 'resource Cannot be find' Now Change your URL As 'http://localhost:60622/Reg/InsertDetails' to get Output.
To avoid this doing manually later tutorials you will see Routing concepts.
To avoid this doing manually later tutorials you will see Routing concepts.
19 Comments
Can't we insert without models in mvc
ReplyDeletemvc iyidir öğren yiğenim. Emrullah Musa TİLKİ İZMİR
ReplyDeletewell said! This content is the right way to enhance your knowledge and I like it this post. I want more new updates and keep posting...!
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Spark Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
Social Media Marketing Courses in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
This is really too useful and have more ideas and keep sharing many techniques. Eagerly waiting for your new blog keep doing more.
ReplyDeleteweb designing training in chennai
Development courses in Chennai
ccna course in Chennai
Best PHP training in chennai
ReactJS Training in Chennai
Web Designing Course in chennai
Web designing training in chennai
This was an awesome post. This time the admin has shared a chock full of information.
ReplyDeleteSpoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
Best Spoken English Classes in Chennai
Best Spoken English Institute in Chennai
IELTS Coaching in Chennai
English Speaking Classes in Mumbai
Spoken English Classes in Anna Nagar
Spoken English Classes in Chennai Anna Nagar
IELTS Classes in Mumbai
This is really impressive post, I am inspired with your post, do post more blogs like this, I am waiting for your blogs.
ReplyDeleteAviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
best aviation academy in Chennai
air hostess academy in Chennai
Airline Courses in Chennai
airport ground staff training in Chennai
I gather the valuable things which help me to move forward.
ReplyDeleteSEO Training in Coimbatore
SEO Course in Coimbatore
SEO Training Course
SEO Training in Bangalore
SEO Course in Bangalore
Best SEO Training in Bangalore
I think choosing a time to look for an apartment is less priority. From this post, I Know that we have to focus on the time to look for the new apartment.
ReplyDeleteFlats for sale in Vadapalani
Apartments for sale in Adyar
Premium apartments in Chennai
New apartments in OMR
Luxury homes in Chennai
This is an awesome post.Really very informative and creative contents.
ReplyDeleteWordpress Development company in Chennai
It was good explanation and it looks more impressive!thank you for sharing precious information with us..
ReplyDeleteIELTS Coaching in Chennai
IELTS Training in Chennai
French Classes in Chennai
pearson vue
Japanese Language Classes in Chennai
Best Spoken English Classes in Chennai
German Language Course in Chennai
IELTS Coaching in Velachery
IELTS Coaching in Tambaram
IELTS Coaching in Anna Nagar
english to punjabi typing
ReplyDeleteweighing machine for kitchen
ReplyDeleteThis was a great blog to explain your readers.
ReplyDeleteweb design and programming courses
php classes in chennai
magento course in chennai
Thanks for giving an Excellent Blog, it's very useful information to us, keep on it doing like this, I eagerly waiting for your updates, Thank you So much...
ReplyDeleteBest Aviation Academy in Chennai
Really informative blog for all people. Thanks for sharing it.
ReplyDeleteSpoken English Classes in Chennai
Spoken English in Chennai
Data Analytics Courses in Chennai
IELTS Coaching centre in Chennai
Japanese Language Classes in Chennai
TOEFL Classes in Chennai
content writing course in chennai
Spoken English Classes in Tambaram
Spoken English Classes in Anna Nagar
It is really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing.
ReplyDeleteProject Management Courses In Hyderabad
I wanted to find more job opportunities in England, because I think here every business is marketing orientated and as you can't do marketing without knowing English. data science course syllabus
ReplyDeleteI am really happy to read your article, I got new information from your blog. Keep updating.
ReplyDeletehow to learn java quickly
mobile application development platforms
creative social media campaigns
what is microsoft azure cloud services
tableau server interview questions
It is really a great and helpful for us . I am glad that you shared this helpful information with us. Please keep us informed like this. thank you
ReplyDelete