user963833
user963833

Reputation: 5

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,OdbcRowUpdatedEventArgs 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'

Upvotes: 0

Views: 325

Answers (3)

Paulo Santos
Paulo Santos

Reputation: 11587

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;  
    }  
} 

Upvotes: 2

Damith
Damith

Reputation: 63105

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 +=

Upvotes: 1

Blazes
Blazes

Reputation: 4779

I think your delegate needs to be static:

static void Adapter_RowUpdated(object sender,OdbcRowUpdatedEventArgs e)

Upvotes: 0

Related Questions