Re: VS.2005 c# Autonumber via Access with @@IDENTITY Failing



Of course the line

OleDbConnection = ta.Connection;

should read

OleDbConnection con = ta.Connection;




"Michael Slattery" wrote:

> Frank,
>
> Still no go...
>
> If I try what you asked...
>
> OleDbConnection = ta.Connection;
> OleDbCommand idCMD = new OleDbCommand("SELECT @@IDENTITY", con);
> int newID = (int)idCMD.ExecuteScalar();
>
> I get the following InvalidOperationException
>
> ExecuteScalar requires an open and available Connection. The connection's
> current state is closed.
>
> Of course I can surround the above code with
> con.open();
> con.close();
>
> but were then back to the original error of being on a different connection...
>
>
>
> "Frank Hickman [MVP]" wrote:
>
> > "Michael Slattery" <sncsoftware@xxxxxxxxxxxxxxxx> wrote in message
> > news:1928F627-97A6-4AC6-B856-FB188ABA464C@xxxxxxxxxxxxxxxx
> > > How would I use the current data adapter to return a value... I've tried
> > > adding parameters as Output and ReturnValue (seperate occassions of
> > > course)
> > > and all I get back is 0.
> > >
> > > "Frank Hickman [MVP]" wrote:
> > >
> >
> > I believe that since your creating a new connection the server variable
> > @@IDENTITY is blank because there has not be any statements executed with
> > the connection object. You can get the connection object that the adapter
> > used by using the InsertCommand property of the adapter. From there you can
> > use the Connection property. Something like this should work for you...
> >
> > // ta is your Database.dbDataSetTableAdapters.tblPeopleTableAdapter
> >
> > System.Data.OleDb.OleDbConnection con= ta.InsertCommand.Connection;
> > OleDbCommand idCMD = new OleDbCommand("SELECT @@IDENTITY", con);
> > int newID = (int)idCMD.ExecuteScalar();
> >
> > HTH
> > --
> > ============
> > Frank Hickman
> > Microsoft MVP
> > NobleSoft, Inc.
> > ============
> > Replace the _nosp@m_ with @ to reply.
> >
> >
> >
.



Relevant Pages