Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

Using Stored Procedures with ASP.NET
planetsourcecode's picture
Language: .NET
Category: Databases

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

0
Your rating: None


Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...