PC Review


Reply
Thread Tools Rate Thread

Autonumber in Access / Dataset question..pls help!!!!!!

 
 
=?Utf-8?B?S2FybCBXaGl0ZQ==?=
Guest
Posts: n/a
 
      25th Nov 2004
if I have a simple Access table like so:

Customer = (customerNo , firstName, lastName)

and the customerNo field has autonumbering set (i.e. is the primary key
value in the table and is a number that increments automatically when a new
record is added).

When I extract all records using a Dataset object. I want to add return the
value of the customerNo if I were to add a new record. So if I added a new
customer to the table I want to return a new customer number.

Do you know to do this >>!!? can the following code be adpated to do this
!?!??!


Public Function getData() As DataSet
Const INT_EMPTY As Integer = 0
Try
If mblnIsDatabaseOpen Then
Dim pstrSQL As String
If sqltext = "" Then Exit Function
Dim pobjAdtAdaptor As New OleDbDataAdapter
Dim pobjdstDataset As New DataSet
Dim sqlText = "SELECT * FOM CUSTOMER;"
pobjAdtAdaptor.SelectCommand = New OleDbCommand(sqltext,
mcnnObjConnection)
pobjdstDataset.Clear()
If pobjAdtAdaptor.Fill(pobjdstDataset, "Data") = INT_EMPTY
Then
getData = Nothing
Else
getData = pobjdstDataset.Tables("Data").DataSet
End If
pobjAdtAdaptor = Nothing
pobjdstDataset = Nothing
Else
MsgBox("Database not open")
End If
Catch ex As Exception
MsgBox("Could not query the database", MsgBoxStyle.Critical)
If mblnIsDatabaseOpen Then closeDatabase()
Exit Function
End Try
End Function

 
Reply With Quote
 
 
 
 
Richard Myers
Guest
Posts: n/a
 
      25th Nov 2004
Hi Karl

SELECT @@IDENTITY

Heres a C# article you should easily be able to pick thru.
http://www.c-sharpcorner.com/Code/20...AutoNumber.asp

Richard

"Karl White" <Karl (E-Mail Removed)> wrote in message
news:82788A7C-CA9F-4CD7-A14A-(E-Mail Removed)...
> if I have a simple Access table like so:
>
> Customer = (customerNo , firstName, lastName)
>
> and the customerNo field has autonumbering set (i.e. is the primary key
> value in the table and is a number that increments automatically when a new
> record is added).
>
> When I extract all records using a Dataset object. I want to add return the
> value of the customerNo if I were to add a new record. So if I added a new
> customer to the table I want to return a new customer number.
>
> Do you know to do this >>!!? can the following code be adpated to do this
> !?!??!
>
>
> Public Function getData() As DataSet
> Const INT_EMPTY As Integer = 0
> Try
> If mblnIsDatabaseOpen Then
> Dim pstrSQL As String
> If sqltext = "" Then Exit Function
> Dim pobjAdtAdaptor As New OleDbDataAdapter
> Dim pobjdstDataset As New DataSet
> Dim sqlText = "SELECT * FOM CUSTOMER;"
> pobjAdtAdaptor.SelectCommand = New OleDbCommand(sqltext,
> mcnnObjConnection)
> pobjdstDataset.Clear()
> If pobjAdtAdaptor.Fill(pobjdstDataset, "Data") = INT_EMPTY
> Then
> getData = Nothing
> Else
> getData = pobjdstDataset.Tables("Data").DataSet
> End If
> pobjAdtAdaptor = Nothing
> pobjdstDataset = Nothing
> Else
> MsgBox("Database not open")
> End If
> Catch ex As Exception
> MsgBox("Could not query the database", MsgBoxStyle.Critical)
> If mblnIsDatabaseOpen Then closeDatabase()
> Exit Function
> End Try
> End Function
>



 
Reply With Quote
 
Richard Myers
Guest
Posts: n/a
 
      25th Nov 2004
Perhaps i should have added A2K and above.


 
Reply With Quote
 
=?Utf-8?B?S2FybCBXaGl0ZQ==?=
Guest
Posts: n/a
 
      25th Nov 2004
I don't understand C##.

Whats @@IDENTITY mean!?!?!

cmd = new OleDbCommand("SELECT @@IDENTITY", conn);
int nId = (int)cmd.ExecuteScalar();


"Richard Myers" wrote:

> Hi Karl
>
> SELECT @@IDENTITY
>
> Heres a C# article you should easily be able to pick thru.
> http://www.c-sharpcorner.com/Code/20...AutoNumber.asp
>
> Richard
>
> "Karl White" <Karl (E-Mail Removed)> wrote in message
> news:82788A7C-CA9F-4CD7-A14A-(E-Mail Removed)...
> > if I have a simple Access table like so:
> >
> > Customer = (customerNo , firstName, lastName)
> >
> > and the customerNo field has autonumbering set (i.e. is the primary key
> > value in the table and is a number that increments automatically when a new
> > record is added).
> >
> > When I extract all records using a Dataset object. I want to add return the
> > value of the customerNo if I were to add a new record. So if I added a new
> > customer to the table I want to return a new customer number.
> >
> > Do you know to do this >>!!? can the following code be adpated to do this
> > !?!??!
> >
> >
> > Public Function getData() As DataSet
> > Const INT_EMPTY As Integer = 0
> > Try
> > If mblnIsDatabaseOpen Then
> > Dim pstrSQL As String
> > If sqltext = "" Then Exit Function
> > Dim pobjAdtAdaptor As New OleDbDataAdapter
> > Dim pobjdstDataset As New DataSet
> > Dim sqlText = "SELECT * FOM CUSTOMER;"
> > pobjAdtAdaptor.SelectCommand = New OleDbCommand(sqltext,
> > mcnnObjConnection)
> > pobjdstDataset.Clear()
> > If pobjAdtAdaptor.Fill(pobjdstDataset, "Data") = INT_EMPTY
> > Then
> > getData = Nothing
> > Else
> > getData = pobjdstDataset.Tables("Data").DataSet
> > End If
> > pobjAdtAdaptor = Nothing
> > pobjdstDataset = Nothing
> > Else
> > MsgBox("Database not open")
> > End If
> > Catch ex As Exception
> > MsgBox("Could not query the database", MsgBoxStyle.Critical)
> > If mblnIsDatabaseOpen Then closeDatabase()
> > Exit Function
> > End Try
> > End Function
> >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?S2FybCBXaGl0ZQ==?=
Guest
Posts: n/a
 
      25th Nov 2004
I don't understand C#.

Whats @@IDENTITY all about !??!

cmd = new OleDbCommand("SELECT @@IDENTITY", conn);
int nId = (int)cmd.ExecuteScalar();

please advise, Many thanks

"Richard Myers" wrote:

> Hi Karl
>
> SELECT @@IDENTITY
>
> Heres a C# article you should easily be able to pick thru.
> http://www.c-sharpcorner.com/Code/20...AutoNumber.asp
>
> Richard
>
> "Karl White" <Karl (E-Mail Removed)> wrote in message
> news:82788A7C-CA9F-4CD7-A14A-(E-Mail Removed)...
> > if I have a simple Access table like so:
> >
> > Customer = (customerNo , firstName, lastName)
> >
> > and the customerNo field has autonumbering set (i.e. is the primary key
> > value in the table and is a number that increments automatically when a new
> > record is added).
> >
> > When I extract all records using a Dataset object. I want to add return the
> > value of the customerNo if I were to add a new record. So if I added a new
> > customer to the table I want to return a new customer number.
> >
> > Do you know to do this >>!!? can the following code be adpated to do this
> > !?!??!
> >
> >
> > Public Function getData() As DataSet
> > Const INT_EMPTY As Integer = 0
> > Try
> > If mblnIsDatabaseOpen Then
> > Dim pstrSQL As String
> > If sqltext = "" Then Exit Function
> > Dim pobjAdtAdaptor As New OleDbDataAdapter
> > Dim pobjdstDataset As New DataSet
> > Dim sqlText = "SELECT * FOM CUSTOMER;"
> > pobjAdtAdaptor.SelectCommand = New OleDbCommand(sqltext,
> > mcnnObjConnection)
> > pobjdstDataset.Clear()
> > If pobjAdtAdaptor.Fill(pobjdstDataset, "Data") = INT_EMPTY
> > Then
> > getData = Nothing
> > Else
> > getData = pobjdstDataset.Tables("Data").DataSet
> > End If
> > pobjAdtAdaptor = Nothing
> > pobjdstDataset = Nothing
> > Else
> > MsgBox("Database not open")
> > End If
> > Catch ex As Exception
> > MsgBox("Could not query the database", MsgBoxStyle.Critical)
> > If mblnIsDatabaseOpen Then closeDatabase()
> > Exit Function
> > End Try
> > End Function
> >

>
>
>

 
Reply With Quote
 
Richard Myers
Guest
Posts: n/a
 
      25th Nov 2004
Hi Karl

You shouldn't have to understand C# for the purposes of reading that article as there is very little
difference in the code shown apart from {} etc when compared to VB.

@@IDENTITY

is an Access construct that allows you to query the Jet engine for the last AutoIncrement value.
Its Access specific not .net/vb/c# specific so you can use it from any platform/language.

So basically you run your insert code to insert the new record as per usual and then
you call the code you quoted below to get the the AutoInc supplied value of your PK column.

Because ExecuteScalar returns an object you then have to type it.
(int)cmd.ExecuteScalar() which is C# for CInt(cmd.ExecuteScalar)...... although
chances are your AutoInc column is of type Long in which case CLng(cmd.ExecuteScalar)
would be more appropriate.

hth
Richard

"Karl White" <Karl (E-Mail Removed)> wrote in message
news:FA5DB769-C451-4A3B-AFCE-(E-Mail Removed)...
> I don't understand C#.
>
> Whats @@IDENTITY all about !??!
>
> cmd = new OleDbCommand("SELECT @@IDENTITY", conn);
> int nId = (int)cmd.ExecuteScalar();
>
> please advise, Many thanks
>
> "Richard Myers" wrote:
>
> > Hi Karl
> >
> > SELECT @@IDENTITY
> >
> > Heres a C# article you should easily be able to pick thru.
> > http://www.c-sharpcorner.com/Code/20...AutoNumber.asp
> >
> > Richard
> >
> > "Karl White" <Karl (E-Mail Removed)> wrote in message
> > news:82788A7C-CA9F-4CD7-A14A-(E-Mail Removed)...
> > > if I have a simple Access table like so:
> > >
> > > Customer = (customerNo , firstName, lastName)
> > >
> > > and the customerNo field has autonumbering set (i.e. is the primary key
> > > value in the table and is a number that increments automatically when a new
> > > record is added).
> > >
> > > When I extract all records using a Dataset object. I want to add return the
> > > value of the customerNo if I were to add a new record. So if I added a new
> > > customer to the table I want to return a new customer number.
> > >
> > > Do you know to do this >>!!? can the following code be adpated to do this
> > > !?!??!
> > >
> > >
> > > Public Function getData() As DataSet
> > > Const INT_EMPTY As Integer = 0
> > > Try
> > > If mblnIsDatabaseOpen Then
> > > Dim pstrSQL As String
> > > If sqltext = "" Then Exit Function
> > > Dim pobjAdtAdaptor As New OleDbDataAdapter
> > > Dim pobjdstDataset As New DataSet
> > > Dim sqlText = "SELECT * FOM CUSTOMER;"
> > > pobjAdtAdaptor.SelectCommand = New OleDbCommand(sqltext,
> > > mcnnObjConnection)
> > > pobjdstDataset.Clear()
> > > If pobjAdtAdaptor.Fill(pobjdstDataset, "Data") = INT_EMPTY
> > > Then
> > > getData = Nothing
> > > Else
> > > getData = pobjdstDataset.Tables("Data").DataSet
> > > End If
> > > pobjAdtAdaptor = Nothing
> > > pobjdstDataset = Nothing
> > > Else
> > > MsgBox("Database not open")
> > > End If
> > > Catch ex As Exception
> > > MsgBox("Could not query the database", MsgBoxStyle.Critical)
> > > If mblnIsDatabaseOpen Then closeDatabase()
> > > Exit Function
> > > End Try
> > > End Function
> > >

> >
> >
> >



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      26th Nov 2004
Karl,

There is in my opinion not much to add to what Richard wrote, however Bill
made a complete article about this on MSDN.

http://msdn.microsoft.com/library/de...anidcrisis.asp

I hope this helps?

Cor

"Karl White" <Karl (E-Mail Removed)>

>I don't understand C#.
>
> Whats @@IDENTITY all about !??!
>
> cmd = new OleDbCommand("SELECT @@IDENTITY", conn);
> int nId = (int)cmd.ExecuteScalar();
>
> please advise, Many thanks
>
> "Richard Myers" wrote:
>
>> Hi Karl
>>
>> SELECT @@IDENTITY
>>
>> Heres a C# article you should easily be able to pick thru.
>> http://www.c-sharpcorner.com/Code/20...AutoNumber.asp
>>
>> Richard
>>
>> "Karl White" <Karl (E-Mail Removed)> wrote in message
>> news:82788A7C-CA9F-4CD7-A14A-(E-Mail Removed)...
>> > if I have a simple Access table like so:
>> >
>> > Customer = (customerNo , firstName, lastName)
>> >
>> > and the customerNo field has autonumbering set (i.e. is the primary key
>> > value in the table and is a number that increments automatically when a
>> > new
>> > record is added).
>> >
>> > When I extract all records using a Dataset object. I want to add return
>> > the
>> > value of the customerNo if I were to add a new record. So if I added a
>> > new
>> > customer to the table I want to return a new customer number.
>> >
>> > Do you know to do this >>!!? can the following code be adpated to do
>> > this
>> > !?!??!
>> >
>> >
>> > Public Function getData() As DataSet
>> > Const INT_EMPTY As Integer = 0
>> > Try
>> > If mblnIsDatabaseOpen Then
>> > Dim pstrSQL As String
>> > If sqltext = "" Then Exit Function
>> > Dim pobjAdtAdaptor As New OleDbDataAdapter
>> > Dim pobjdstDataset As New DataSet
>> > Dim sqlText = "SELECT * FOM CUSTOMER;"
>> > pobjAdtAdaptor.SelectCommand = New
>> > OleDbCommand(sqltext,
>> > mcnnObjConnection)
>> > pobjdstDataset.Clear()
>> > If pobjAdtAdaptor.Fill(pobjdstDataset, "Data") =
>> > INT_EMPTY
>> > Then
>> > getData = Nothing
>> > Else
>> > getData = pobjdstDataset.Tables("Data").DataSet
>> > End If
>> > pobjAdtAdaptor = Nothing
>> > pobjdstDataset = Nothing
>> > Else
>> > MsgBox("Database not open")
>> > End If
>> > Catch ex As Exception
>> > MsgBox("Could not query the database",
>> > MsgBoxStyle.Critical)
>> > If mblnIsDatabaseOpen Then closeDatabase()
>> > Exit Function
>> > End Try
>> > End Function
>> >

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
basic MS Access Question.. get a data into a dataset Aussie Rules Microsoft VB .NET 3 16th Feb 2008 05:51 PM
AutoNumber question - MySQL for Access Forms =?Utf-8?B?R2FyeSBEb2xsaXZlcg==?= Microsoft Access Form Coding 1 2nd Nov 2006 08:34 AM
Generated Strongly Typed Dataset and AutoNumber =?Utf-8?B?UmVtaUBuZXdzZ3JvdXBzLm5vc3BhbQ==?= Microsoft ADO .NET 1 6th Mar 2006 08:41 PM
Database, DataSet, AutoNumber,.... Help ?? Hai Nguyen Microsoft ASP .NET 3 10th Jan 2004 01:25 AM
Database, DataSet, AutoNumber,.... Help ?? Hai Nguyen Microsoft ADO .NET 1 9th Jan 2004 09:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:48 PM.