Saving data from a listbox bounded to a database in c#
I have a dataset bounded to a listbox in a Windows CE application using SQL Server CE.
There is only one table in the dataset named "Codigos" with two fields: an id and a "code" field.
I inserted manually some data into my table and loads fine into the listbox.
The problem happens when I try to add a new value to the listbox 开发者_C百科and then save it.
This is the code I've tried:
DatalogicDataSet.CodigosRow newCodigosRow = datalogicDataSet.Codigos.NewCodigosRow();
newCodigosRow.codigo = "003";
datalogicDataSet.Codigos.Rows.Add(newCodigosRow);
This loads a new row into the listbox with the "003" value.
I associated to a click event in a button the code to update the database:
private void button1_Click(object sender, EventArgs e)
{
datalogicDataSet.AcceptChanges();
}
I also tried this:
private void button1_Click(object sender, EventArgs e)
{
try
{
this.codigosBindingSource.EndEdit();
this.codigosTableAdapter.Update(this.datalogicDataSet.Codigos);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}
}
But neither of them worked, the database is not updated.
Any suggestions?
Try:
private void button1_Click(object sender, EventArgs e)
{
DatalogicDataSet.CodigosRow newCodigosRow = datalogicDataSet.Codigos.NewCodigosRow();
newCodigosRow.codigo = "003";
datalogicDataSet.Codigos.Rows.Add(newCodigosRow);
datalogicDataSet.AcceptChanges();
}
The solution in case anyone happens to find the same problem.
The problem was that the database was being updated correctly, but not the one in my computer, only the one in the terminal with windows CE. This happens because the application copies the database into the Windows CE terminal device when it's executed through Visual Studio 2008.
So each time I executed the application it loaded the database in my computer with the original data instead of the updated data, so it seemed to work wrong, but the one in the terminal device was being correctly updated.
When you generate your production program and install it into the device with Windows CE it won't have that problem and will update the database correctly.
精彩评论