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) ENDaspx.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();
}
}
}
Currently rated 4.67 by 3 people
Rate Now!
Date Posted:
03 April 2007 14:16
Last Updated:
12 May 2007 23:12
Posted by:
Mikesdotnetting
Total Views to date:
5926
Printer Friendly Version



18 January 2010 21:07 from 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".