Hello,
I wrote a little program to understand the ADO.NET's mechanism. It's a common program with a datagrid, textboxes and a toolstripbar with usually buttons. The problem is that meanwhile i add rows to dataset the save button works when i remove some of them the update method doesn't work.
Please see the following code:
public Popola()
{
InitializeComponent();
string connstring = "Data Source = .\\SQLEXPRESS; Initial Catalog = scuola; Integrated Security = True";
conn = new SqlConnection(connstring);
myset = new DataSet("Classe");
myadapter = new SqlDataAdapter("SELECT * FROM Classe ORDER BY IDClasse ASC", conn);
myadapter.TableMappings.Add("Table", "Classe");
myadapter.Fill(myset);
da2 = new SqlDataAdapter("SELECT * FROM Studente ORDER BY Cognome ASC", conn);
da2.TableMappings.Add("Table", "Studente");
da2.Fill(myset);
DataColumn colMaster1;
DataColumn colDetail1;
colMaster1 = myset.Tables["Classe"].Columns["IDClasse"];
colDetail1 = myset.Tables["Studente"].Columns["IDClasse"];
relClasStud = new DataRelation("RelClasStud", colMaster1, colDetail1);
myset.Relations.Add(relClasStud);
myset.EndInit();
dsView = myset.DefaultViewManager;
.......
cm_clas = (CurrencyManager)this.BindingContext[dsView, "Classe"];
cm = (CurrencyManager)this.BindingContext[dsView, "Classe.RelClasStud"];
}
//Remove and Save buttons code:
private void Remove_Click(object sender, EventArgs e)
{
DataRowView rw_ = (DataRowView)cm.Current;
DataRow rw = rw_.Row;
myset.Tables["Studente"].Rows.Remove(rw);
cm.EndCurrentEdit();
//myset.AcceptChanges();
cm.Position = 0;
textBoxPosition.Text = String.Format("Record {0} of {1}", (cm.Position + 1), cm.Count);
}
private void btnSalva_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommandBuilder sqlcmd = new SqlCommandBuilder(da2);
SqlCommandBuilder sqlcmd1 = new SqlCommandBuilder(myadapter);
da2.Update(myset.Tables["Studente"]);
myadapter.Update(myset.Tables["Classe"]);
conn.Close();
}
Can you help me? T.T
Regards.
Pietro.