Here is my Solution Explorer for my project.

The following is my Connection.cs:

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using Forms.Models;
  7. namespace Forms.Models
  8. {
  9. public class Connection
  10. {
  11. public SqlCommand cmdData = new SqlCommand();
  12. public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
  13. public SqlDataAdapter dataAdapter = new SqlDataAdapter();
  14. DataTable dt = new DataTable();
  15. private string connStr = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
  16. public Boolean funSPNonExecuteData(String SPName, SqlParameter[] sqlParams)
  17. {
  18. SqlCommand cmdData = new SqlCommand();
  19. cmdData.CommandText = SPName;
  20. cmdData.CommandType = CommandType.StoredProcedure;
  21. cmdData.Connection = funGetConnection();
  22. for (int i = 0; i < sqlParams.Length - 1; i++)
  23. {
  24. cmdData.Parameters.AddWithValue(sqlParams[i].ParameterName, sqlParams[i].Value);
  25. }
  26. cmdData.ExecuteNonQuery();
  27. return true;
  28. }

  29. public SqlConnection funGetConnection()
    {
    if (conn.State == ConnectionState.Closed)
    {
    conn.Open();
    }
    return conn;
    }
  30. }
  31. }
The following is the Registration Model Class:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using MVCPROJECT.Models;
  8. using Forms.Models;
  9. namespace MVCPROJECT.Models
  10. {
  11. public class Registration
  12. {
  13. Connection objConnection = new Connection();#region "Properties"
  14. SqlDataAdapter ds = new SqlDataAdapter();
  15. DataTable dt = new DataTable();
  16. public string Fname {get;set;}
  17. public string SName {get;set;}
  18. public string Lname {get;set;}
  19. public string Id {get;set;}
  20. public string EmailId {get;set;}
  21. public string Password {get;set;}
  22. #endregion
  23. public bool Registration1(string id, string pass, string fname, string sname, string lname, string email)
  24. {
  25. bool res = false;
  26. SqlParameter[] sp = new SqlParameter[6];
  27. sp[0] = new SqlParameter("@UserId", id);
  28. sp[1] = new SqlParameter("@password", pass);
  29. sp[2] = new SqlParameter("@fname", fname);
  30. sp[3] = new SqlParameter("@sname", sname);
  31. sp[4] = new SqlParameter("@lname", lname);
  32. sp[5] = new SqlParameter("@email", email);
  33. res = objConnection.funSPNonExecuteData("dbo.sp_registration", sp);
  34. if (res == true)
  35. {
  36. return res;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. }
  44. }
Here is my database structure:



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:

  1. @model MVCPROJECT.Models.Registration
  2. @{
  3. ViewBag.Title = "Registration";
  4. }
  5. <div class="panel-heading" style="margin-left:5%;">
  6. <h2 class="panel-title" >User Registration Form</h2>
  7. </div>
  8. <br />
  9. <br />
  10. <div class="panel panel-default" style="margin-left:5%;height:600px">
  11. @using(Html.BeginForm("Registration","Home"))
  12. {
  13. <div class="panel-body">
  14. <span class="label label-default">First Name</span>
  15. <div>
  16. <input type="text" class="form-control" placeholder="First Name" name="Fname" aria-describedby="sizing-addon1">
  17. </div>
  18. <span class="label label-default">Middle Name</span>
  19. <div>
  20. <input type="text" class="form-control" placeholder="Middle Name" name="sname" aria-describedby="sizing-addon1">
  21. </div>
  22. <span class="label label-default">Last Name</span>
  23. <div>
  24. <input type="text" class="form-control" placeholder="Last name" name="lname" aria-describedby="sizing-addon1">
  25. </div>
  26. <span class="label label-default">User Id</span>
  27. <div>
  28. <input type="text" class="form-control" placeholder="User Id" name="id" aria-describedby="sizing-addon1">
  29. </div>
  30. <span class="label label-default">Password</span>
  31. <div>
  32. <input type="text" class="form-control" placeholder="Password" name="password" aria-describedby="sizing-addon1">
  33. </div>
  34. <span class="label label-default">Email Id</span>
  35. <div>
  36. <input type="text" class="form-control" placeholder="Email Id" name="EmailId" aria-describedby="sizing-addon1">
  37. </div>
  38. <div>
  39. <input id="btn_save" type="submit" value="Save" name="Save" />
  40. <input id="btn_cancel" type="submit" value="Cancel" name="Cancel" />
  41. </div>
  42. </div>
  43. }
  44. </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:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MVCPROJECT.Models;
  7. namespace MVCPROJECT.Controllers
  8. {
  9. public class HomeController: Controller
  10. {
  11. Registration reg = new Registration();
  12. [HttpGet]
  13. public ActionResult Registration()
  14. {
  15. return View();
  16. }
  17. [HttpPost]
  18. public ActionResult Registration(FormCollection fc, string name)
  19. {
  20. if (name == "Save") //to check which button is clicked save or cancel
  21. {
  22. reg.Id = fc["Id"];
  23. reg.Fname = fc["Fname"];
  24. reg.SName = fc["SName"];
  25. reg.Lname = fc["Lname"];
  26. reg.Password = fc["Password"];
  27. reg.EmailId = fc["EmailId"];
  28. bool res = false;
  29. res = reg.Registration1(reg.Id, reg.Password, reg.Fname, reg.SName, reg.Lname, reg.EmailId);
  30. if (res == true)
  31. {
  32. Response.Write("Registration Done successfully");
  33. }
  34. else
  35. {
  36. Response.Write("Please Try again");
  37. }
  38. }
  39. else
  40. {
  41. Response.Write("data Cancelled");
  42. }
  43. return View();
  44. }
  45. }
  46. }
Whatever value the user has inserted comes to the controller using the form collection element.
From here we retrieve a seperate seperate property and then send it to a method in the model called Registration1. From registration1 it will add the value to the Stored Procedure and execute.

And thus it will start working.



Click Save and check the Registration table.