Here is my Solution Explorer for my project.

- I have added a controller called Homecontroller.
- Added 2 classes to my Model Folder.
- My connection class in my model contains the connection string with all the operation for CRUD operations. Here I am showing this class.
- The Register class in the model contains all the database fields with the logic.
The following is my Connection.cs:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using Forms.Models;
- namespace Forms.Models
- {
- public class Connection
- {
- public SqlCommand cmdData = new SqlCommand();
- public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
- public SqlDataAdapter dataAdapter = new SqlDataAdapter();
- DataTable dt = new DataTable();
- private string connStr = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
- public Boolean funSPNonExecuteData(String SPName, SqlParameter[] sqlParams)
- {
- SqlCommand cmdData = new SqlCommand();
- cmdData.CommandText = SPName;
- cmdData.CommandType = CommandType.StoredProcedure;
- cmdData.Connection = funGetConnection();
- for (int i = 0; i < sqlParams.Length - 1; i++)
- {
- cmdData.Parameters.AddWithValue(sqlParams[i].ParameterName, sqlParams[i].Value);
- }
- cmdData.ExecuteNonQuery();
- return true;
- }
-
public SqlConnection funGetConnection(){if (conn.State == ConnectionState.Closed){conn.Open();}return conn;}
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data;
- using System.Data.SqlClient;
- using MVCPROJECT.Models;
- using Forms.Models;
- namespace MVCPROJECT.Models
- {
- public class Registration
- {
- Connection objConnection = new Connection();#region "Properties"
- SqlDataAdapter ds = new SqlDataAdapter();
- DataTable dt = new DataTable();
- public string Fname {get;set;}
- public string SName {get;set;}
- public string Lname {get;set;}
- public string Id {get;set;}
- public string EmailId {get;set;}
- public string Password {get;set;}
- #endregion
- public bool Registration1(string id, string pass, string fname, string sname, string lname, string email)
- {
- bool res = false;
- SqlParameter[] sp = new SqlParameter[6];
- sp[0] = new SqlParameter("@UserId", id);
- sp[1] = new SqlParameter("@password", pass);
- sp[2] = new SqlParameter("@fname", fname);
- sp[3] = new SqlParameter("@sname", sname);
- sp[4] = new SqlParameter("@lname", lname);
- sp[5] = new SqlParameter("@email", email);
- res = objConnection.funSPNonExecuteData("dbo.sp_registration", sp);
- if (res == true)
- {
- return res;
- }
- else
- {
- return false;
- }
- }
- }
- }

The following is the Stored Procedure:

After all this is my View.
When creating the view choose strongly typed view and the model as Registration.

And the HTML code for my view is:
- @model MVCPROJECT.Models.Registration
- @{
- ViewBag.Title = "Registration";
- }
- <div class="panel-heading" style="margin-left:5%;">
- <h2 class="panel-title" >User Registration Form</h2>
- </div>
- <br />
- <br />
- <div class="panel panel-default" style="margin-left:5%;height:600px">
- @using(Html.BeginForm("Registration","Home"))
- {
- <div class="panel-body">
- <span class="label label-default">First Name</span>
- <div>
- <input type="text" class="form-control" placeholder="First Name" name="Fname" aria-describedby="sizing-addon1">
- </div>
- <span class="label label-default">Middle Name</span>
- <div>
- <input type="text" class="form-control" placeholder="Middle Name" name="sname" aria-describedby="sizing-addon1">
- </div>
- <span class="label label-default">Last Name</span>
- <div>
- <input type="text" class="form-control" placeholder="Last name" name="lname" aria-describedby="sizing-addon1">
- </div>
- <span class="label label-default">User Id</span>
- <div>
- <input type="text" class="form-control" placeholder="User Id" name="id" aria-describedby="sizing-addon1">
- </div>
- <span class="label label-default">Password</span>
- <div>
- <input type="text" class="form-control" placeholder="Password" name="password" aria-describedby="sizing-addon1">
- </div>
- <span class="label label-default">Email Id</span>
- <div>
- <input type="text" class="form-control" placeholder="Email Id" name="EmailId" aria-describedby="sizing-addon1">
- </div>
- <div>
- <input id="btn_save" type="submit" value="Save" name="Save" />
- <input id="btn_cancel" type="submit" value="Cancel" name="Cancel" />
- </div>
- </div>
- }
- </div>
Note here that the name of the controls should be the same as the the registration model property.
This form will post to the Home control and Registration action methods.
Add a home controller.
Add a Registration action method and write the following code.
The following is my Home controller:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVCPROJECT.Models;
- namespace MVCPROJECT.Controllers
- {
- public class HomeController: Controller
- {
- Registration reg = new Registration();
- [HttpGet]
- public ActionResult Registration()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Registration(FormCollection fc, string name)
- {
- if (name == "Save") //to check which button is clicked save or cancel
- {
- reg.Id = fc["Id"];
- reg.Fname = fc["Fname"];
- reg.SName = fc["SName"];
- reg.Lname = fc["Lname"];
- reg.Password = fc["Password"];
- reg.EmailId = fc["EmailId"];
- bool res = false;
- res = reg.Registration1(reg.Id, reg.Password, reg.Fname, reg.SName, reg.Lname, reg.EmailId);
- if (res == true)
- {
- Response.Write("Registration Done successfully");
- }
- else
- {
- Response.Write("Please Try again");
- }
- }
- else
- {
- Response.Write("data Cancelled");
- }
- return View();
- }
- }
- }
And thus it will start working.

Click Save and check the Registration table.
![]()

Sanoj YadavPosted May 24, 2015, 11:54 AM
nice.......
Karthik Muthu KaruppanPosted May 4, 2015, 2:03 PM
nice
Gowtham RajamanickamPosted May 2, 2015, 4:48 AM
good
Santhakumar MunuswamyPosted May 2, 2015, 3:06 AM
Good work
Debendra DashPosted May 2, 2015, 1:49 AM
Thanks Nitin..
NitinPosted May 2, 2015, 1:43 AM
good one