insert,update,delete
insert,update,delete code in windows form using back end sqlserver2005.pls.
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 Mar 29, 2013, 8:59 AM
Jignesh TrivediPosted Mar 29, 2013, 8:48 AM
hi,
please refer below link it might help you.
http://nareshkamuni.blogspot.com/2012/04/how-to-insert-edit-update-and-delete.html
http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-insert-edit-update-and-delete--the-ado.net-way.aspx
Vishal GilbilePosted Mar 29, 2013, 7:59 AM
Lets consider I've an employee Table with fields (Empid,EName,EAddress,ESalary) in my Database and I want to fire insert,update and delete query on that Table. Following code will do the same.
Insert Code
SqlConnection con=new SqlConnection("Your Connection String");
SqlCommand cmd=new SqlCommand("Insert into Emp values(@name,@add,@sal)",con);
cmd.Parameters.AddWithValue("@name",txtName.Text);
cmd.Parameters.AddWithValue("@add",txtAdd.Text);
cmd.Parameters.AddWithValue("@sal",txtSal.Text);
con.Open();
int rows=cmd.ExecuteNonQuery();
if(rows>0)
{
MessageBox.Show("Data saved Successfully","Data saved");
con.Close();
}
Update Code
SqlConnection con=new SqlConnection("Your Connection String");
SqlCommand cmd=new SqlCommand("update Emp Set EName=@name, EAddress=@add, Esalary=@sal where Empid=@id",con);
cmd.Parameters.AddWithValue("@name",txtName.Text);
cmd.Parameters.AddWithValue("@add",txtAdd.Text);
cmd.Parameters.AddWithValue("@sal",txtSal.Text);
cmd.Parameters.AddWithValue("@id",txtID.Text);
con.Open();
int rows=cmd.ExecuteNonQuery();
if(rows>0)
{
MessageBox.Show("Data Updated Successfully","Data saved");
con.Close();
}
Delete Code
SqlConnection con=new SqlConnection("Your Connection String");
SqlCommand cmd=new SqlCommand("Delete From Emp where Empid=@id",con);
cmd.Parameters.AddWithValue("@id",txtID.Text);
con.Open();
int rows=cmd.ExecuteNonQuery();
if(rows>0)
{
MessageBox.Show("Data Updated Successfully","Data saved");
con.Close();
}
Hope that Solves your problem.
With Regards,
Vishal Gilbile.