Insert Data Into Database by Stored Procedure in ASP.Net C#

This article shows how to insert data into a database using ASP.Net C# and a Stored Procedure. I used several textboxes and a button. When the user clicks on the “Insert” button the data is saved into the database.

INITIAL CHAMBER

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (insert_demo).

Step 2

In Solution Explorer you get your empty website, then add a Web Form and SQL Server Database as in the following.

For Web Form:

insert_demo (Your Empty Website) then right-click then select Add New Item -> Web Form. Name it insertdata_demo.aspx.

For SQL Server Database:

insert_demo (Your Empty Website) then right-click then select Add New Item -> SQL Server Database. (Add the database inside the App_Data_folder.)

DATABASE CHAMBER

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this:

Table -> tbl_data (Don't Forget to make ID as IS Identity -- True)


Figure 1: Data Table

Make one Stored Procedure for inserting data into the database, by going to database.mdf then seelct Store Procedures then right-click then select Add New Store Procedure.

Sp_insert


Figure 2: Sp Insert

DESIGN CHAMBER

Step 4

Now make some design for your application by going to insertdata_demo.aspx and try the code like this.

insertdata_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style type="text/css">  
  8. .style1  
  9. {  
  10. width: 139px;  
  11. }  
  12. .style2  
  13. {  
  14. width: 158px;  
  15. }  
  16. .style3  
  17. {  
  18. width: 139px;  
  19. height: 23px;  
  20. }  
  21. .style4  
  22. {  
  23. width: 158px;  
  24. height: 23px;  
  25. }  
  26. .style5  
  27. {  
  28. height: 23px;  
  29. }  
  30. .style6  
  31. {  
  32. font-size: large;  
  33. text-decoration: underline;  
  34. }  
  35. </style>  
  36.     </head>  
  37.     <body>  
  38.         <form id="form1" runat="server">  
  39.             <div>  
  40.                 <strong>  
  41.                     <span class="style6">Insert Data into Database</span>  
  42.                 </strong>  
  43.                 <br />  
  44.                 <table style="width:100%;">  
  45.                     <tr>  
  46.                         <td class="style1">  
  47.  </td>  
  48.                         <td class="style2">  
  49.  </td>  
  50.                         <td>  
  51.  </td>  
  52.                     </tr>  
  53.                     <tr>  
  54.                         <td class="style1">  
  55. Name</td>  
  56.                         <td class="style2">  
  57.                             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  58.                         </td>  
  59.                         <td>  
  60.  </td>  
  61.                     </tr>  
  62.                     <tr>  
  63.                         <td class="style1">  
  64. Education</td>  
  65.                         <td class="style2">  
  66.                             <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  67.                         </td>  
  68.                         <td>  
  69.  </td>  
  70.                     </tr>  
  71.                     <tr>  
  72.                         <td class="style1">  
  73. Email</td>  
  74.                         <td class="style2">  
  75.                             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  76.                         </td>  
  77.                         <td>  
  78.  </td>  
  79.                     </tr>  
  80.                     <tr>  
  81.                         <td class="style3">  
  82. Phoneno</td>  
  83.                         <td class="style4">  
  84.                             <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
  85.                         </td>  
  86.                         <td class="style5"></td>  
  87.                     </tr>  
  88.                     <tr>  
  89.                         <td class="style1">  
  90. City</td>  
  91.                         <td class="style2">  
  92.                             <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>  
  93.                         </td>  
  94.                         <td>  
  95.  </td>  
  96.                     </tr>  
  97.                     <tr>  
  98.                         <td class="style1">  
  99.  </td>  
  100.                         <td class="style2">  
  101.  </td>  
  102.                         <td>  
  103.  </td>  
  104.                     </tr>  
  105.                     <tr>  
  106.                         <td class="style1">  
  107.  </td>  
  108.                         <td class="style2">  
  109.                             <asp:Button ID="Button1" runat="server" BorderColor="#CCFF66"   
  110. ForeColor="#0066FF" onclick="Button1_Click" Text="Insert Data" />  
  111.                         </td>  
  112.                         <td>  
  113.                             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  114.                         </td>  
  115.                     </tr>  
  116.                 </table>  
  117.             </div>  
  118.         </form>  
  119.     </body>  
  120. </html>  

Your design will look like this:


Figure 3: Design Page

CODE CHAMBER

Step 5

Now it's time for server-side coding so that our application works. Open your insertdata_demo.aspx.cs file and code it as in the following.

insertdata_demo.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. public partial class _Default: System.Web.UI.Page   
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.     }  
  14.     protected void Button1_Click(object sender, EventArgs e)  
  15.     {  
  16.         SqlConnection con = new SqlConnection(@  
  17.         "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  18.         SqlCommand cmd = new SqlCommand("sp_insert", con);  
  19.         cmd.CommandType = CommandType.StoredProcedure;  
  20.         cmd.Parameters.AddWithValue("name", TextBox1.Text);  
  21.         cmd.Parameters.AddWithValue("email", TextBox2.Text);  
  22.         cmd.Parameters.AddWithValue("education", TextBox3.Text);  
  23.         cmd.Parameters.AddWithValue("phoneno", TextBox4.Text);  
  24.         cmd.Parameters.AddWithValue("city", TextBox5.Text);  
  25.         con.Open();  
  26.         int k = cmd.ExecuteNonQuery();  
  27.         if (k != 0) {  
  28.             lblmsg.Text = "Record Inserted Succesfully into the Database";  
  29.             lblmsg.ForeColor = System.Drawing.Color.CornflowerBlue;  
  30.         }  
  31.         con.Close();  
  32.     }  
OUTPUT CHAMBER


Figure 4: Output


Figure 5: Insert Data


Figure 6: Save Data

I hope you like it, thank you for the reading. Have a good Day.

Up Next
    Ebook Download
    View all
    Learn
    View all