SIGN UP MEMBER LOGIN:    
ARTICLE

Inserting records into a database table using a stored procedure and ADO.NET

Posted by Rohatash Kumar Articles | ADO.NET in C# May 02, 2011
This article demonstrates how to use ASP.NET and ADO.NET with Visual C# .NET to create and to call a Microsoft SQL Server stored procedure.
Reader Level:
Download Files:
 


This article demonstrates how to use ASP.NET and ADO.NET with Visual C# .NET to create and to call a Microsoft SQL Server stored procedure.

Stored Procedure

A stored procedure is a batch of Transact-SQL statements (like select, insert and update) compiled into a single execution that can be re-used. If you find yourself using the same query over and over again, it would make sense to put them into a stored procedure. Every time you write a query it is parsed in the database. If you have written a stored procedure for it, it will be compiled once and can be executed multiple times.

There are two steps involved in executing a stored procedure from your program. First, you set the command object property CommandText as the stored procedure name; second, you set the CommandType property as CommandType.StoredProcedure.

Step 1

Now we Create a table in a database named logintab and it has the three columns Login, Password and Id.
The Database table looks like the Figure 1 shown below.

store1.gif

Figure 1

Step 2

Now we create a stored procedure in the database named storlog. The stored procedure looks like Figure 2.

store2.gif

Figure 2

Step 3: Calling stored procedure

Taking three TextBoxes, one Button and a Label control on the form, the form looks like this.

store3.gif

Figure 3

Now double-click on the Save button control and add the following code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

 

namespace StoreProcedure

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void ButtonSave_Click(object sender, EventArgs e)

        {

            string str = "Data Source=.;uid=sa;pwd=Password$2;database=master";

            SqlConnection con = new SqlConnection(str);

            con.Open();

            SqlCommand com = new SqlCommand("storlog", con);

            com.Parameters.Add("@username", SqlDbType.VarChar).Value = TextBoxUsername .Text;

            com.Parameters.Add("@Password", SqlDbType.VarChar).Value = TextBoxPassword.Text;

            com.Parameters.Add("@Id", SqlDbType.Int).Value = TextBoxId.Text ;

            com.CommandType = CommandType.StoredProcedure;

            com.ExecuteNonQuery();

            Label1 .Text= "record has been saved";

            con.Close();

        }

    }

}

The above code defines the connection with the database and command object property CommandText as the stored procedure name; second, you set the CommandType property as CommandType.StoredProcedure.

Step 4

Now run the application and enter the username, password and Id in the textboxes.

The form looks like this.

store4.gif

Figure 4

Step 5

Now click on the save Button. All record has been saved in the database.

store5.gif

Figure 5

Step 6

Now open the database and test it.

store6.gif

Figure 6

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

you can write stoprocedure for insert, delete,update on sqlserver and in visual C#, you use sqlcommand with commandType is CommandType.Procedure. But you must know storeprocedure. ex: create proc spdelete as declare @masv varchar(5) delete tblNhanVien where manv = @manv and visual C#. sqlcommand cmd = new sqlcommand(spdelete,con); cmd.commandtype = commandtype.procedure; cmd.parametr.add(@manv,sqltype.varchar(5)).value = txtManv.text.Trim(); cmd.executenonquery();

Posted by snake vietnam May 28, 2011

To add the data in database this code really works..Please help me if u can send me the code for delete and update also continuation with this add code.

Posted by Surabhi Bansal May 04, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor