Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2004, 05:34 AM
Authorized User
 
Join Date: Jul 2003
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Default Handling AutoNumbers in Dataset

How do we handle autonumbers in VBNet?

Do we include it in our Select Statements?
if Yes:
  What happens when we update it?
If No:
  How do we get the values


Proud To Be Pinoy
__________________
Proud To Be Pinoy
 
Old December 15th, 2004, 09:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'm not sure what you mean. Are you asking how to retrieve them?

 
Old December 15th, 2004, 11:59 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Just like any other program that deals with a database system that allows autonumbered fields, you have to use and insert statement that does not specify a value for that field. When you update, you can not update that field (which you normally would never do anyway because usually the autonumber field is the or part of a key to a record).

You can select from the autonumber field normally.
 
Old December 25th, 2004, 10:35 AM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

Dear reyboy!

You must not assign a value to an AutoNumber field in your inserts or updates. Doing so will surely throw an exeption for you 2 catch.

You can select an AutoNumber field as you would fields that were not AutoNumber. If you are using DataAdapter and DataSet in VB.Net then there's something you should be aware of. Here it goes:

In order to make your DataSet synchronized with the underlying Data Source in regard to a AutoNumber field you will need to use a stored procedure for your insert which has an output parameter that selects the AutoNumber just generated by the most recent insert. In code it looks like this

assume u have this stored proc in Northwind databse (SQL Server) for the products table. the productid is an AutoNumber field:

CREATE PROC spAddProduct
    @ProductName NVARCHAR(40),
    @ProductID INT OUTPUT
AS
INSERT INTO Products (ProductName)
VALUES (@ProductName)

SELECT @ProductID = @@Identity
GO

Now from VB.Net, here is how u'd configure a data set and a data adapter

Dim productsAdapter As New SqlDataAdapter
Dim nwindDataSet As New DataSet

' configure select cmd....

' configure update cmd....

' configure delete cmd....

' configure INSERT cmd....
Dim prm As SqlParameter
Dim cmdInsert As New SqlCommand
cmdInsert = conn.CreateCommand()
cmdInsert.CommandText = "spAddProduct"
cmdInsert.CommandType = CommandType.StoredProcedure
prm = cmdInsert.Parameters.Add("@ProductName", SqlDbType.NVarChar, 40, "ProductName")
prm = cmdInsert.Parameters.Add("@ProductID", SqlDbType.Int)
prm.SourceColumn = "ProductID"
prm.Direction = ParameterDirection.Output
productsAdapter.InsertCommand = cmdInsert

' now fill the data set
productsAdapter.Fill(nwindDataSet, "Products")

' display results in data grid
productsDataGrid.DataSource = nwindDataSet
productsDataGrid.DataMember = "Products"

And after you add new rows to the data grid and save them back the data grid will automatically sho you the new AutoNumber productID.
To save data back use this code

productsAdapter.Update(nwindDataSet)

Plz notice how we configured the insert command. we explicitly told the data adapter that the @ProductID parameter was an output parameter by using this statement

prm.Direction = ParameterDirection.Output

This ensures that the DataAdapter passes the newly generated ProductID (which is an AutoNumber) back to the DataSet.which is finally displayed in the DataGrid.

I hope it would help u learn how to use AutoNumber with DataAdapter, DataSet and DataGrid.



ejan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill a DataSet from another DataSet kamranzafar C# 1 February 7th, 2007 11:14 AM
Fill(dataset) or dataset.load() salemkoten SQL Server 2005 1 November 2nd, 2006 11:04 PM
Converting a untyped dataset to a typed dataset daphnean Visual Studio 2005 0 July 13th, 2006 01:16 AM
Handling an empty dataset Payback Classic ASP Basics 2 January 21st, 2005 12:42 PM
Re: SQL Server dataset to ACCESS dataset dazzer ADO.NET 0 March 22nd, 2004 05:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.