I try updating my database after updating the dataset, but evenafter calling the data adapter's update function the underlying database doesn't change. I used SqlCommandBuilder and it still wouldn't update. Once I have the GUI open I can submit and search for values in my dataset but once I close it then re run it, all the changes are gone. Help?
Code:
//in my form load event
con = new System.Data.SqlClient.SqlConnection();
ds1 = new DataSet();
ds2 = new DataSet();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";
con.Open();
MessageBox.Show("Database Open.");
string sql = "SELECT * From tblPurchases";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
string cust = "SELECT * From tblCustomers";
da2 = new System.Data.SqlClient.SqlDataAdapter(cust, con);
da.Fill(ds1, "Purchases");
numOfRecords = ds1.Tables["Purchases"].Rows.Count;
da2.Fill(ds2, "Customers");
numOfCustomers = ds2.Tables["Customers"].Rows.Count;
con.Close();
MessageBox.Show("Database Closed.");
Code:
//in my submit button click event
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
System.Data.SqlClient.SqlCommandBuilder cb2;
cb2 = new System.Data.SqlClient.SqlCommandBuilder(da2);
DataRow dRow = ds1.Tables["Purchases"].NewRow();
DataRow dRow2 = ds2.Tables["Customers"].NewRow();
//dRow updating happens here
ds1.Tables["Purchases"].Rows.Add(dRow);
ds2.Tables["Customers"].Rows.Add(dRow2);
da.Update(ds1, "Purchases");
da2.Update(ds2, "Customers");
That's it. There are no error messages, it's just that even with that, the underlying database doesn't update and the updates only go as far as the dataset.