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: 35323

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

Borut Spanovic 5/29/2015 11:28 AM
In response to Using ASP.NET Identity with Razor Web Pages
From this great tutorial is past a lot of time. I wonder is something change for WebMatrix - is it...

Ansuya 5/29/2015 8:24 AM
In response to Displaying Google Analytics Data in ASP.NET
Above code is not working....

Joseph Casey 5/28/2015 7:51 PM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Can you explain or provide a resource as to what is happening here? public enum FileType { =...

cesar 5/28/2015 4:56 PM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
When the application pool recycle then the quartz scheduler is also destroyed, how do we keep it are...

Steve 5/28/2015 4:44 PM
In response to MVC 5 with EF 6 in Visual Basic - Reading Related Data
On this page under the section "Disable lazy loading before serialization" , the "Advanced Scenarios...

Manie Verster 5/28/2015 9:39 AM
In response to MVC 5 with EF 6 in Visual Basic - Implementing Inheritance
Sorry Mike but on my project when run it does not show the hire date and/or enrollment date. I the I...

Joseph Casey 5/27/2015 3:17 PM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Probably should have read the title. Couldn't use it with Visual Studios 2013. =[...

jean 5/27/2015 8:41 AM
In response to iTextSharp - Drawing shapes and Graphics
hey Mike, I read your arcticle about drawing on PDF with ITextSharp, it was really interesting !...

Alexandros L 5/26/2015 10:50 PM
In response to Entity Framework 6 Recipe - Hierarchical Data Management
You are a live saver.. thank you so much......

saurabh rao 5/26/2015 1:03 PM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Hey Mike ! Great article ...the code worked flawlessly for me. Didn't have any issues executing it ....