Add Autoincrement Column to a databound dataset

Rattlesnake

Well-known member
Joined
Dec 23, 2003
Messages
47
Hi
I have a master detail form (in ASP.net) that displays Purchase Order (PO) header and its details (Items).

I load a dataset with the PO detail rows of the Purchase ORder (from a SQL database) and assign it as a datasource to a datagrid.

For programming purposes (to track changes to each row) I add an Integer colum "ID" (autoincrement=true) to this Dataset AFTER I load it with the PO detail rows . (note:this column is not from the database table , but manually added )

The problem I face is that this column (ID) has NULL value for already existing rows (i.e. retrieved from the database). The field starts from 1 only for rows that are added after the page is displayed.

Is there a way to have this ID column have values for already exisitng rows plus the newly added rows.

Thanks
 
there are 2 ways of going about doing this
1) modify the sql such.
"SELECT @id:=0;SELECT @id:=@id+1 AS id, po.* FROM po"
its a mysql query but it adds a prepends a column to the resultset of an incrementing integer starting at zero.

2) using ado.net. Im trying to figure this one out myself so Ill have to get back to you on that.
 
ok, heres code of how to do it using a data adapter.
[VB]
using(GenericDbConnection conn = new GenericDbConnection(Database.ConnectionInfo))
{
using(GenericDbDataAdapter da = new GenericDbDataAdapter(Database.ConnectionInfo))
{
using(da.SelectCommand = conn.CreateCommand())
{
da.SelectCommand.CommandText = "SELECT * FROM personal";
using(DataTable dt = new DataTable("personal"))
{
dt.Columns.Add("id", typeof(int)).AutoIncrement = true;
da.Fill(dt);
}
}
}
}
[/VB]
 
Back
Top