Loading

Using Stored Procedures with ASP.NET

Submitted by: 


Stored procedures are series of SQL statements grouped into a single unit, we can pass parameters to the stored procedures same like the passing parameters to the functions
Simple stored procedure

  1. USE [databasename]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[user_insert_mycomment] Script Date: 11/19/2008 22:45:11 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. CREATE PROCEDURE [dbo].[user_insert_mycomment]
  10. -- Add the parameters for the stored procedure here
  11. (
  12. @comment VARCHAR(500),
  13. @eventID BIGINT
  14. )
  15. AS
  16. BEGIN
  17. BEGIN TRY
  18.  
  19. Insert into mycomment(eventID, comment)
  20. values(@eventID, @comment)
  21.  
  22. END TRY
  23. BEGIN CATCH
  24. PRINT ERROR_MESSAGE()
  25. END CATCH
  26. END

This stored procedure is taking two input parameters first is “comment” and second is “eventID” , these values are inserting into “mycomment” table
The next thing is that how we call this stored procedure into our asp.net page, so we are moving into next things now

Step1: Add 2 labels, 2 textbox and one command button into the page and change the property of these controls according to the following list

  1. 1.) TextBox1,change Property - ID=”tbEventID”
  2. 2.) TextBox2,change Property - Text=”tbComment”
  3. 3.) Label1,change Property - Text=”event”
  4. 4.) Label2,change Property - Text=”comment”
  5. 5.) Command button 1, change text to submit

Step2: double click on the command button and write the below code in it

  1. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StrCon"].ToString());
  2.  
  3. SqlCommand cmd = new SqlCommand("NAME_OF_STORED_PROCEDURE ", con);
  4. cmd.CommandType = CommandType.StoredProcedure;
  5.  
  6. // Parameters list
  7. cmd.Parameters.Clear();
  8. cmd.Parameters.Add("@eventID", SqlDbType.BigInt).Value = Request.QueryString["id"];
  9. cmd.Parameters.Add("@comment", SqlDbType.VarChar, 500).Value = tbComment.Text.Trim();
  10.  
  11. // end parameters
  12.  
  13. con.Open();
  14. cmd.ExecuteNonQuery();
  15. con.Close();

Explanation:
1) SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StrCon"].ToString());

The above line will create a connection name”con”

2) SqlCommand cmd = new SqlCommand("NAME_OF_STORED_PROCEDURE ", con);
cmd.CommandType = CommandType.StoredProcedure;

This will create command object with name of the stored procedure in it and then we have to declare the type of command in our case i.e. StoredProcedure

3). cmd.Parameters.Clear();
The above line will clear all the parmaters

4) cmd.Parameters.Add("@eventID", SqlDbType.BigInt).Value = Request.QueryString["id"];
cmd.Parameters.Add("@comment", SqlDbType.VarChar, 500).Value = tbComment.Text.Trim();

the above paramters command will used to pass paramters (eventID and Comment) to the stored procedure

5) con.Open();
cmd.ExecuteNonQuery();
con.Close();

above three line will open the connection, run the stored procedure and then in last close the connection

the above article will give you the basic of using a stored procedure with asp.net

About the author:

PlanetSourceCode.in is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development




Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.