Wednesday 18 January 2012

stored procedure for inserting records

first create the procedure in the sql server like this

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[comment]
(
@name varchar(50),
@email varchar(50),
@comment varchar(max)
)
as
insert into Commentbox (SE_name,SE_Email,SE_comment) values (@name,@email,@comment)

and then in code behind

 string connstr = ConfigurationManager.ConnectionStrings["smartedu"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "dbo.comment";
 
        cmd.Parameters.AddWithValue("@name",TextBox1.Text);
        cmd.Parameters.AddWithValue("@email", TextBox2.Text);
        cmd.Parameters.AddWithValue("@comment", TextBox3.Text);
        cmd.Connection = conn;

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }

        TextBox3.Text = "";
        TextBox2.Text = "";
        TextBox1.Text = "";

No comments:

Post a Comment