Unable to update changes to database after editing the gridview. There are no errors thrown, but changes are not updated in table in database. I am using Sql server here and I have two Winforms called Form1 and Form 2. A button(btnSearchAll in Form1) opens the gridview in Form2 with all the data loaded. When I try to edit the data in gridview(in Form2) and tried to update, it is not updating the changes. Please help
Below is my Form 1:
namespace Tailor_app
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
SqlDataAdapter da;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
txtFirstName.Focus();
da = new SqlDataAdapter("select * from Measurement", con);
da.Fill(ds, "Measurement");
dt = ds.Tables["Measurement"];
SqlCommandBuilder cb = new SqlCommandBuilder(da);
cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
}
private void btnSave_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["CellNumber"] = txtCellNo.Text.Trim();
dr["FirstName"] = txtFirstName.Text;
dr["LastName"] = txtLastName.Text;
dr["Shirt"] = txtShirt.Text;
dr["Pant"] = txtPant.Text;
dr["DueDate"] = txtDueDate.Text;
dr["Date"] = txtDate.Text;
if (dr["CellNumber"] == "")
{
MessageBox.Show("Please enter Cell Number");
}
else if (dr["CellNumber"] != "")
{
dt.Rows.Add(dr);
MessageBox.Show("Success");
}
try
{
da.Update(ds, "Measurement");
}
catch (DBConcurrencyException ex)
{
MessageBox.Show(ex.Message);
dt.Clear();
da.Fill(ds, "Measurement");
}
}
private void btnSearchAllCustomers_Click(object sender, EventArgs e)
{
this.Hide();
frmDgv_SearchResult frm2 = new frmDgv_SearchResult();
frm2.Show();
using (da = new SqlDataAdapter())
{
try
{
da.SelectCommand = new SqlCommand("Select * From Measurement", con);
ds.Clear();
da.Fill(ds,"Measurement");
dt = ds.Tables["Measurement"];
frm2.dgvSearchResults.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
}
Below is my Form 2:
namespace Tailor_app
{
public partial class frmDgv_SearchResult : Form
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
SqlDataAdapter da;
DataTable dt;
public frmDgv_SearchResult()
{
InitializeComponent();
}
private void dgvSearchResults_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void frmDgv_SearchResult_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from Measurement", con);
da.Fill(ds, "Measurement");
dt = ds.Tables["Measurement"];
SqlCommandBuilder cb = new SqlCommandBuilder(da);
cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
da.Update(ds, "Measurement");
}
catch (DBConcurrencyException ex)
{
MessageBox.Show(ex.Message);
dt.Clear();
da.Fill(ds, "Measurement");
}
finally
{
MessageBox.Show("success");
}
}
Try requesting the Update commands for your Data Adapter :-