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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Aug 31, 2011, 12:38 AM
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 DiamondPosted Aug 31, 2011, 4:47 AM
Thanks for your response and the code, it's work perfectly.