Hi everyone,
How to update sql database without using dataadapter?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 30, 2011, 8:38 PM EDT
con.Open();
str = "update student set sname='" + txtname.Text.Trim() + "',smarks=" + txtmarks.Text.Trim() + ",saddress='" + txtaddress.Text.Trim() + "',year='" + txtyear.Text.Trim() + "' where sid='" + txtid.Text.Trim() + "'";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
----------------------------------------------------------------------------------------------------------------------------
con.Open();
str = "update student set sname=@sname,smarks=@smarks,saddress=@saddress,year=@year where sid=@sid";
com = new SqlCommand(str, con);
com.Parameters.Add("@sid", txtid.Text);
com.Parameters.Add("@sname", txtname.Text);
com.Parameters.Add("@smarks", txtmarks.Text);
com.Parameters.Add("@saddress", txtaddress.Text);
com.Parameters.Add("@year", txtyear.Text);
com.ExecuteNonQuery();
con.Close();
------------------------------------------------------------------------------------------------------------------------------
Stored Procedure
CREATE PROCEDURE update1
(@sid varchar(50),@sname varchar(50),@smarks int,@saddress varchar (50),@year varchar(50))
AS
update student set sname=@sname,smarks=@smarks,saddress=@saddress,year=@year where sid=@sid
con.Open();
com = new SqlCommand("update1", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@sid", txtid.Text);
com.Parameters.Add("@sname", txtname.Text);
com.Parameters.Add("@smarks", int.Parse(txtmarks.Text));
com.Parameters.Add("@saddress", txtaddress.Text);
com.Parameters.Add("@year", txtyear.Text);
com.ExecuteNonQuery();
con.Close();
------------------------------------------------------------------------------------------------------------------------------------
Thanks
If this post helps you mark it as answer
Black Diamond191 reputationPosted Aug 31, 2011, 12:47 AM EDT
Thanks for your response and the code, it's work perfectly.