add new record with autoincrement field error

slluch

Member
Joined
Apr 19, 2008
Messages
6
Programming Experience
5-10
Hi. I have (I suppose) a typical problem. I have a details form and I want to add a new record by clicking the add button on a bindingnavigator. I've read a lot of forums with the same problem but i don see any clear solution. The problem is that the primary key of the table is autoincrement so i don't know its value and i don't want to get the next id because if other user makes the same action (add row) one of them will get an error (right?). The problem i'm getting is that before I make an update to the underlying dataset i suppose that is necessary to call endEdit method of the bindingsource, but i get an error because of the null value of the primary key. I tried to add a dummy value (for example 0) to the primary key and that works fine because the db engine assigns an autoincremented value. The problem is that there are other nested bindingsources bounded to a relation of the underlying dataset and one of it's primarykey fields is the new autoincrement field.
Any ideas?

Thank you
 
I've never had a problem:

Parent is autoincrement
Child related to parent is NOT autoincrement
Create new parent, gets auto ID (note to EndEdit parent here to avoid a .net bug)
Create new child, child takes current autoID (Note to EndEdit child here to avoid a .net bug)
Save parent
DB calculates new parent ID
Parent DataTable retrives new parent ID automatically (your db must support multiple statemnts per command, SQL, oracle.. see the tickbox in the TableAdapter config)
Child sees change, and takes new ID also
Save child

Everyone is happy
 
Hi, thanks for your answer.
I'm not using wizards to build datasets. I have a business logic layer that rerieves "manually" the data and returns a dataset with all tables and relations. Working this way when I add a new item to the parent bindingsource, the primarykey field is empty so when i call EndEdit method i get an error. I read that a column in a dataset can be "autoincrement" (I suppose that this is what you mean with autoID?), but i get an error:

obtenerDetalleArticulos = New DataSet

MyBase.SQL = "select ta.*, c_stock.sumaStock,c_stock.stockDisponible,c_stock.pendienteCliente,c_stock.pendienteProveedor from TARTICULOS ta,c_articulos_con_stock_pend_cliente_y_pend_prov c_stock where 1=1 " _
& filtroActivos _
& " and ta.idArticulo=c_stock.idArticulo" _
& filtro & " order by ta.descripcion"
MyBase.InitializeCommand()

'primarykey
MyBase.FillDataSet(obtenerDetalleArticulos, "TARTICULOS")
Dim id(0) As DataColumn
id(0) = obtenerDetalleArticulos.Tables("TARTICULOS").Columns("idArticulo")
id(0).AutoIncrement = True
obtenerDetalleArticulos.Tables("TARTICULOS").PrimaryKey = id

The error is that a field with type UINT32 can´t be autoincrement!!. I'm using MySQL as DB and in the table definition the primarykey has autoincrement flag.

Thank you
 

Latest posts

Back
Top