Insert Single Value With Stored Procedure ASP.NET C#

Code for calling stored procedure in the Code-Behind to insert a single value from a text box.

aspx:
<asp:Label ID="Label1" runat="server" Visible="false"></asp:Label><br />
Add Category: <asp:TextBox ID="txtCategory" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Stored Procedure:
CREATE PROCEDURE [dbo].[AddCategory] 
  @Category nvarchar(20)
AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;

  INSERT INTO CATEGORIES (Category) VALUES (@Category)
END
aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
string ArticleType = txtArticleType.Text;
string connectionString = Utils.GetConnString();
        
using (SqlConnection conn = new SqlConnection(connectionString))
  {	
			
   using (SqlCommand cmd = new SqlCommand("AddArticleType", conn))
    {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ArticleType", ArticleType);
    conn.Open();
    cmd.ExecuteNonQuery();
    }
  }
}

Date Posted:
Last Updated:
Posted by:
Total Views to date: 35606

3 Comments

- Taner Ozdas

You have a typo erron in your code example.

using (SqlCommand cmd = new SqlCommand("AddArticleType", conn))


should be ;

using (SqlCommand cmd = new SqlCommand("AddCategory", conn))

because you create stored procedure with a name "AddCategory".

- Tabish Sagheer

This code is very Helpfull for me. Thanks for given this code.

- nirali

nice

Recent Comments

Semil 7/1/2015 7:03 AM
In response to iTextSharp - Drawing shapes and Graphics
I have created a rectangle using above methode. Now I want to add a text in the center of this How I...

Satyabrata Mohapatra 6/30/2015 6:12 PM
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Ahh.....this is awesome. Happy to see after a long time you wrote a article on web form :D...

Marty 6/30/2015 7:16 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Mike, you're the Man! Another great article. So incredibly helpful. I'm definitely going to buy your...

Rohan 6/30/2015 5:32 AM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Very good and helpful tutorial. Thanks. Just wanted to know what would be the max file size limit we...

Fernando 6/30/2015 1:59 AM
In response to Programmatically accessing data from DataSource controls
What if I want to pass parameters natively using the DataSourceSelectArguments object, instead of be...

pankaj 6/29/2015 3:13 PM
In response to How to retain carriage returns or line breaks in an ASP.NET web page
very nice i'm use this in my code thank you.... ...

Mike 6/29/2015 2:22 AM
In response to MVC 5 with EF 6 in Visual Basic - Sorting, Filtering and Paging
This is the first example that I have found that works....

Marty 6/28/2015 4:57 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Mike, what if I don't want to render back the text to the browser, but I want to send it some other...

Mike 6/27/2015 4:00 PM
In response to Migrating Classic ASP To ASP.NET Razor Web Pages Part One- Razor Syntax And Visual Basic
have you used any of the code converters to convert classic asp to c#? If so, which one do you have...

deepan 6/27/2015 12:49 PM
In response to How To Send Email In ASP.NET MVC
Your article is very very useful for me...thanks for giving this article ...this is very simple to u...