Error when using OleDB or ODBC in C# application
I have a windows application and I have this code:
private void saveToDatabase_Click(object sender, EventArgs e)
{
// Save the DataSet Appointments table to the database.
KaznetiTableAdapter ta = new KaznetiTableAdapter();
ta.Adapter.RowUpdated += new OleDbRowUpdatedEventHandler(Adapter_RowUpdated);
ta.Update(kbDataSet.Kazneti);
}
void Adapter_RowUpdated(object sender,OdbcRowUpdated开发者_如何学PythonEventArgs e)
{
if (e.RecordsAffected == 0)
{
MessageBox.Show(
e.Row["Adresa"].ToString()
"Optimistic Concurrency Error - Notes Not Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
e.Status = UpdateStatus.SkipCurrentRow;
}
}
I got an error message:
Error 1 No overload for 'Adapter_RowUpdated' matches delegate 'System.Data.OleDb.OleDbRowUpdatedEventHandler'
If I change OleDb in the bolded code in Odbc I got an error again:
Error 1 Cannot implicitly convert type 'System.Data.Odbc.OdbcRowUpdatedEventHandler' to 'System.Data.OleDb.OleDbRowUpdatedEventHandler'
I guess that the error message you're getting is pretty obvious:
Cannot implicitly convert type 'System.Data.Odbc.OdbcRowUpdatedEventHandler' to 'System.Data.OleDb.OleDbRowUpdatedEventHandler'
So, change the line
void Adapter_RowUpdated(object sender,OdbcRowUpdatedEventArgs e)
to
void Adapter_RowUpdated(object sender,OleDbRowUpdatedEventArgs e)
Edited to answer a comment
Then I think you could try something like this:
ta.Adapter.RowUpdated += (sender, e) =>
{
if (e.RecordsAffected == 0)
{
MessageBox.Show(
e.Row["Adresa"].ToString()
"Optimistic Concurrency Error - Notes Not Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Warning );
e.Status = UpdateStatus.SkipCurrentRow;
}
}
if you using OleDbDataAdapter
adapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
create handler as
protected static void OnRowUpdating(object sender,
OleDbRowUpdatingEventArgs args)
{
// code
}
if you using SqlDataAdapter
adapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating );
create handler as
private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
// code
}
you can easily generate event on visual studio by pressing tab key twice ones you type +=
I think your delegate needs to be static:
static void Adapter_RowUpdated(object sender,OdbcRowUpdatedEventArgs e)
精彩评论