CODE FOR INSERT AND UPDATE ON SINGLE BUTTON AND DELETE CODE WITH DEMO APPLICATION TO DOWNLOAD GO HERE :-
Download Application for sql insert , Update , delete using c#
try
{
SqlCommand cmd1 = new SqlCommand("select * from Agency_detail where Agency_code=" + textBox1.Text, con);
SqlDataAdapter da1 = new SqlDataAdapter();
DataTable dt1 = new DataTable();
da1.SelectCommand = cmd1;
da1.Fill(dt1);
if ((dt1.Rows.Count > 0) && (dt1.Rows[0][0].ToString() != ""))
{
if (MessageBox.Show("ID Already Exist Do you want to Update It", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlCommand cmdupdate = new SqlCommand("update Agency_detail set Agency_Name=@p1,Phone=@p2,fax=@p3,Mobile_Number=@p4,DOJ=@p5,opening=@p6,Remark=@p7,Address=@p8,city=@p9,state=@p10,zip_code=@p11,email=@p12 where Agency_code=" + textBox1.Text, this.con );
cmdupdate.Parameters.AddWithValue("@p1", textBox2.Text);
cmdupdate.Parameters.AddWithValue("@p2", textBox4.Text);
cmdupdate.Parameters.AddWithValue("@p3", textBox3.Text);
cmdupdate.Parameters.AddWithValue("@p4", textBox8.Text);
cmdupdate.Parameters.AddWithValue("@p5", dateTimePicker1.Value.Date);
cmdupdate.Parameters.AddWithValue("@p6", textBox6.Text);
cmdupdate.Parameters.AddWithValue("@p7", textBox5.Text);
cmdupdate.Parameters.AddWithValue("@p8", textBox17.Text);
cmdupdate.Parameters.AddWithValue("@p9", textBox16.Text);
cmdupdate.Parameters.AddWithValue("@p10", textBox15.Text);
cmdupdate.Parameters.AddWithValue("@p11", textBox14.Text);
cmdupdate.Parameters.AddWithValue("@p12", textBox13.Text);
//con.con.Open();
cmdupdate.ExecuteNonQuery();
MessageBox.Show("Updated");
retrieve_data();
clearall();
}
else
{
}
}
else
{
if ((textBox1.Text != "") && (textBox2.Text != ""))
{
SqlCommand cmd = new SqlCommand("insert into Agency_detail values(@para1,@para2,@para3,@para4,@para5,@para6,@para7,@para8,@para9,@para10,@para11,@para12,@para13)", con);
cmd.Parameters.AddWithValue("@para1", Convert.ToInt64(textBox1.Text));
cmd.Parameters.AddWithValue("@para2", textBox2.Text);
cmd.Parameters.AddWithValue("@para3", Convert.ToInt64(textBox4.Text));
cmd.Parameters.AddWithValue("@para4", Convert.ToInt64(textBox3.Text));
cmd.Parameters.AddWithValue("@para5", Convert.ToInt64(textBox8.Text));
cmd.Parameters.AddWithValue("@para6", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@para7", Convert.ToDouble(textBox6.Text));
cmd.Parameters.AddWithValue("@para8", textBox5.Text);
cmd.Parameters.AddWithValue("@para9", textBox17.Text);
cmd.Parameters.AddWithValue("@para10", textBox16.Text);
cmd.Parameters.AddWithValue("@para11", textBox15.Text);
cmd.Parameters.AddWithValue("@para12", Convert.ToInt64(textBox14.Text));
cmd.Parameters.AddWithValue("@para13", textBox13.Text);
//con.con.Open();
cmd.ExecuteNonQuery();
retrieve_data();
clearall();
}
else { MessageBox.Show("Agency ID and Name Must Required"); }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}