Retrieving @@IDENTITY value from Access Database give always 0 !!!!

  • Thread starter £ukasz Margielewski
  • Start date
£

£ukasz Margielewski

I need to retrieve automunber ID from source access table after inserting
new row...
I tried to do this by this way:
gRUPATableAdapter.Update(dataSetGrafik.GRUPA);

oleDbConnectionForID.Open();

OleDbConnection oleDbConnectionForID = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Grafik\\GrafikApplication\\Import\\baza
Access\\grafikStruktura.mdb;Persist Security Info=False");

OleDbCommand command = new OleDbCommand("SELECT @@IDENTITY",
oleDbConnectionForID);

IDnewAddedGRUPARow = (int)command.ExecuteScalar(); // why it always give 0
????

oleDbConnectionForID.Close();



But it always gives me nothing... please, help me.. Previously i tried tho
use standard solution my attending RowUpdated event (tutorial from MSDN
website). This approach unfortunately cases that compliter gives my warning:

Warning 5 The event 'GrafikApplication.GRUPATableAdapter.RowUpdated' is
never used c:\Grafik\GrafikApplication\DataSetGrafik2.cs 71 67

I use VS C# Express - and it generics a lot of codes, declaration of events,
like rowchanged, itd.. but I cannnot find where I can correctly attend
RowUpdated event ? Is anyone who can help me ??
 
R

Ron Allen

You need to retrieve the Identity field using the same connection that
the update is on and the connection should remain open between the two
operations.
Open your connection for the DataAdapter before calling the Update
method and then construct the new command using that connection. You can
then close it afterwards.

Ron Allen
 
P

Paul Clement

¤ I need to retrieve automunber ID from source access table after inserting
¤ new row...
¤ I tried to do this by this way:
¤ gRUPATableAdapter.Update(dataSetGrafik.GRUPA);
¤
¤ oleDbConnectionForID.Open();
¤
¤ OleDbConnection oleDbConnectionForID = new
¤ OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
¤ Source=C:\\Grafik\\GrafikApplication\\Import\\baza
¤ Access\\grafikStruktura.mdb;Persist Security Info=False");
¤
¤ OleDbCommand command = new OleDbCommand("SELECT @@IDENTITY",
¤ oleDbConnectionForID);
¤
¤ IDnewAddedGRUPARow = (int)command.ExecuteScalar(); // why it always give 0
¤ ????
¤
¤ oleDbConnectionForID.Close();
¤
¤
¤
¤ But it always gives me nothing... please, help me.. Previously i tried tho
¤ use standard solution my attending RowUpdated event (tutorial from MSDN
¤ website). This approach unfortunately cases that compliter gives my warning:
¤
¤ Warning 5 The event 'GrafikApplication.GRUPATableAdapter.RowUpdated' is
¤ never used c:\Grafik\GrafikApplication\DataSetGrafik2.cs 71 67
¤
¤ I use VS C# Express - and it generics a lot of codes, declaration of events,
¤ like rowchanged, itd.. but I cannnot find where I can correctly attend
¤ RowUpdated event ? Is anyone who can help me ??

See the following MS KB article:

HOW TO: Retrieve the Identity Value While Inserting Records into Access Database By Using Visual C#
..NET
http://support.microsoft.com/kb/816112/EN-US/


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Top