Retrieving Data Using QueryString Parameter and Inserting in ASP.Net MVC 3

Introduction



There are the following three ways to pass a value or data from a view to an action:

  • Parameterized Query.
  • Form Data.
  • Model Binding.

A QueryString is the data passed using the browser URL.

  • What we will do in this application.
  • We will learn how to retrieve the data using a QueryString parameter.
  • For all this operation we will use the EntityFramework.

(The database script is available in the attached.)

Create this table inside your database.

  1. create table tbl_QueryStringUse  
  2. (  
  3. ID int identity primary key,  
  4. Name varchar(50),  
  5. Mobile bigint,  
  6. EmailID varchar(100),  
  7. Address varchar(100)  
  8. )  
Start Visual Studio 2010 then Start a new MVC 3 (with Razor View Engine).

Add a new Controller, the project is named “UseQueryString”.


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace MvcApplication3.Controllers  
  8. {  
  9.     public class UseQueryStringController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /UseQueryString/  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.     }  
  20. }  
Add a View to the Index() action method. 




After adding the View:

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2>Index</h2>  
  6. <h6>We will be learcn here how we can Retrieve data from a view by using Query String.</h6>  
  7. <h6>@Html.ActionLink("Add New Rocord", "Insert", "UseQueryString")</h6>  
Explanation 

Run the project and test the output.

Add a new action method to the controller.



Add a new action method named “Insert()”.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace MvcApplication3.Controllers  
  8. {  
  9.     public class UseQueryStringController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /UseQueryString/  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         public ActionResult Insert()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.     }  
  25. }  
Add a new view to the action method. 



After adding the View:

  1. @{  
  2.     ViewBag.Title = "Insert";  
  3. }  
  4.   
  5. <h2>Insert</h2>  
  6. <h6>@Html.ActionLink("Go Back", "Index", "UseQueryString")</h6>  
  7. <h6>Insert New Records Here:</h6>  
Design the form. 

insert new record



Note: To get a value with a Query String, we need to declare the form method as Get only.

Design the Form: “Insert.cshtml” 

  1. @{  
  2.     ViewBag.Title = "Insert";  
  3. }  
  4.       
  5. <h2>Insert</h2>  
  6. <h6>@Html.ActionLink("Go Back", "Index", "UseQueryString")</h6>  
  7. <h6>Insert New Records Here:</h6>  
  8. @using (@Html.BeginForm("InsertNew", "UseQueryString", FormMethod.Get))  
  9. {   
  10.     <table>  
  11.         <tr>  
  12.             <td>Enter Your Name</td><td>:</td><td>@Html.TextBox("TxtName")</td>  
  13.         </tr>  
  14.         <tr>  
  15.             <td>Enter Your Mobile No</td><td>:</td><td>@Html.TextBox("TxtMobile")</td>  
  16.         </tr>  
  17.         <tr>  
  18.             <td>Enter Your EmailID</td><td>:</td><td>@Html.TextBox("TxtEmailID")</td>  
  19.         </tr>  
  20.         <tr>  
  21.             <td>Enter Your Address</td><td>:</td><td>@Html.TextBox("TxtAddress")</td>  
  22.         </tr>  
  23.         <tr>  
  24.             <td>Save Your Details</td><td>:</td><td><input type="submit" value="Submit" /></td>  
  25.         </tr>  
  26.     </table>  
  27. }  
Run the project and watch the output. 



If you now click on the Submit button it will show you the error.


Now create the table if it is not created. 

  1. create table tbl_QueryStringUse  
  2. (  
  3. ID int identity primary key,  
  4. Name varchar(50),  
  5. Mobile bigint,  
  6. EmailID varchar(100),  
  7. Address varchar(100)  
  8. )  
Add this table to the Entity Framework using the following procedure.

Double-click in the Model.edmx file.



Add this table to the Model designer.



Update the Model from the database.



Update Wizard



Select the table name here.



Watch it.



After adding, build the project.

At the Controller Action Method create the Table Object.



Write the code for inserting the data into the Controller. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace MvcApplication3.Controllers  
  8. {  
  9.     public class UseQueryStringController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /UseQueryString/  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         public ActionResult Insert()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.         public ActionResult InsertNew()  
  25.         {  
  26.             // here we need to retrieve the values from the HTML controlls (View Page)  
  27.   
  28.             // Declare some variables to hold the data  
  29.             string _Name, _EmailID, _Address;  
  30.             long _MpbileNo;  
  31.             //  
  32.             //  
  33.             _Name = Request.QueryString["TxtName"];  
  34.             // Converting the datatype  
  35.             _MpbileNo = Convert.ToInt64(Request.QueryString["TxtMobile"]);  
  36.             //  
  37.             _EmailID = Request.QueryString["TxtEmailID"];  
  38.             _Address = Request.QueryString["TxtAddress"];  
  39.             //  
  40.             //Then saving data to the database by using Entity Framework  
  41.   
  42.             using (MVC_PracticeEntities Entity = new MVC_PracticeEntities())  
  43.             {   
  44.                 // Craete the Table and it's Object  
  45.                 tbl_QueryStringUse TblObj = new tbl_QueryStringUse();  
  46.                 //  
  47.                 TblObj.Name = _Name;  
  48.                 TblObj.Mobile = _MpbileNo;  
  49.                 TblObj.EmailID = _EmailID;  
  50.                 TblObj.Address = _Address;  
  51.                 //  
  52.                 //assign the data  
  53.                 Entity.AddTotbl_QueryStringUse(TblObj);  
  54.                 //  
  55.                 //save changes  
  56.                 int Row = Entity.SaveChanges();  
  57.                 if (Row > 0)  
  58.                 {  
  59.                     ViewBag.Msg = " Data Of "+ _Name + " Has been Saved Successfully. ";  
  60.                 }  
  61.             }  
  62.             return View();  
  63.   
  64.         }  
  65.   
  66.     }  
  67. }  
Let's run the project and test it. 



Add some data and save it.



Output



Add some more data and save it.

Go the SQL Server Management Studio and verify the results.



Just a minute.



I am providing all the code again.

Index.cshtml 

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2>Index</h2>  
  6. <h6>We will be learcn here how we can Retrieve data from a view by using Query String.</h6>  
  7. <h6>@Html.ActionLink("Add New Rocord", "Insert", "UseQueryString")</h6>  
Insert.cshtml
  1. @{  
  2.     ViewBag.Title = "Insert";  
  3. }  
  4.   
  5. <h2>Insert</h2>  
  6. <h6>@Html.ActionLink("Go Back", "Index", "UseQueryString")</h6>  
  7. <h6>Insert New Records Here:</h6>  
  8. @using (@Html.BeginForm("InsertNew", "UseQueryString", FormMethod.Get))  
  9. {   
  10.     <table>  
  11.         <tr>  
  12.             <td>Enter Your Name</td><td>:</td><td>@Html.TextBox("TxtName")</td>  
  13.         </tr>  
  14.         <tr>  
  15.             <td>Enter Your Mobile No</td><td>:</td><td>@Html.TextBox("TxtMobile")</td>  
  16.         </tr>  
  17.         <tr>  
  18.             <td>Enter Your EmailID</td><td>:</td><td>@Html.TextBox("TxtEmailID")</td>  
  19.         </tr>  
  20.         <tr>  
  21.             <td>Enter Your Address</td><td>:</td><td>@Html.TextBox("TxtAddress")</td>  
  22.         </tr>  
  23.         <tr>  
  24.             <td>Save Your Details</td><td>:</td><td><input type="submit" value="Submit" /></td>  
  25.         </tr>  
  26.     </table>  
  27. }  
InsertNew.cshtml
  1. @{  
  2.     ViewBag.Title = "InsertNew";  
  3. }  
  4.   
  5. <h2>InsertNew</h2>  
  6. <h6>@Html.ActionLink("Go Back", "Insert", "UseQueryString")</h6>  
  7. <h5>New Record Addedd Successfully.</h5>  
  8.   
  9. <h6>@ViewBag.Msg</h6>  
UseQueryStringController.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace MvcApplication3.Controllers  
  8. {  
  9.     public class UseQueryStringController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /UseQueryString/  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         public ActionResult Insert()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.         public ActionResult InsertNew()  
  25.         {  
  26.             // here we need to retrieve the values from the HTML controlls (View Page)  
  27.   
  28.             // Declare some variables to hold the data  
  29.             string _Name, _EmailID, _Address;  
  30.             long _MpbileNo;  
  31.             //  
  32.             //  
  33.             _Name = Request.QueryString["TxtName"];  
  34.             // Converting the datatype  
  35.             _MpbileNo = Convert.ToInt64(Request.QueryString["TxtMobile"]);  
  36.             //  
  37.             _EmailID = Request.QueryString["TxtEmailID"];  
  38.             _Address = Request.QueryString["TxtAddress"];  
  39.             //  
  40.             //Then saving data to the database by using Entity Framework  
  41.   
  42.             using (MVC_PracticeEntities Entity = new MVC_PracticeEntities())  
  43.             {   
  44.                 // Craete the Table and it's Object  
  45.                 tbl_QueryStringUse TblObj = new tbl_QueryStringUse();  
  46.                 //  
  47.                 TblObj.Name = _Name;  
  48.                 TblObj.Mobile = _MpbileNo;  
  49.                 TblObj.EmailID = _EmailID;  
  50.                 TblObj.Address = _Address;  
  51.                 //  
  52.                 //assign the data  
  53.                 Entity.AddTotbl_QueryStringUse(TblObj);  
  54.                 //  
  55.                 //save changes  
  56.                 int Row = Entity.SaveChanges();  
  57.                 if (Row > 0)  
  58.                 {  
  59.                     ViewBag.Msg = " Data Of "+ _Name + " Has been Saved Successfully. ";  
  60.                 }  
  61.             }  
  62.             return View();  
  63.   
  64.         }  
  65.   
  66.     }  
  67. }