protected void btnAdd_Click(object sender, EventArgs e)
{
String conStr = ConfigurationManager.ConnectionStrings["connectionStringTest"].ConnectionString;
using (SqlConnection con = new SqlConnection(conStr))
{
lblUniqueID.Text = Guid.NewGuid().ToString();
using (SqlCommand cmd = new SqlCommand("AddTest1", con))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = txtName.Text;
cmd.Parameters.Add("@uniqueID", System.Data.SqlDbType.VarChar).Value = lblUniqueID.Text;
con.Open();
int a = cmd.ExecuteNonQuery();
con.Close();
}
}
}
-----------*************----------------
below is the code for Stored procedure
CREATE PROCEDURE AddTest1
@name varchar(50),
@uniqueID varchar(50)
AS
BEGIN
INSERT INTO TEST1
VALUES(@name,@uniqueID)
END
-------------------------**************-----------------------
below is the web.config code
<configuration>
<connectionStrings>
<add name="connectionStringTest" connectionString="initial catalog=test;user=sa1;password=sa;data source=." />
</connectionStrings>
</configuration>
---------------------****************------------------------
below is the code for UI
<div>
<asp:Label ID="lblName" runat="server" Text="Name" />
<asp:TextBox ID="txtName" runat="server" />
<asp:Label runat="server" ID="lblUniqueID" />
<asp:Button runat="server" ID="btnAdd" Text="Add Test with stored procedure" OnClick="btnAdd_Click" />
</div>
---------------*******************----------------------------------
Below screenshot database table
reference:-https://stackoverflow.com/questions/7542517/call-a-stored-procedure-with-parameter-in-c-sharp
No comments:
Post a Comment