unique ID's

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

is there an easy way to generate unique ID's for a DataSet Table or am I
just going to have to generate a random number and search the db to see if
it's been used already?
 
Chuck,

depending on how you are using the DataTable, you probably don't even need
to worry about it. If you are writing back to the DB, the DataAdapter will
take care of this issue through it's UpdateCommand.

However, if you need to generate the unique number in the code, here is the
process for setting up a new DataTable that automaticaly increments an ID
column.

If you bind the resultant DataTable to a DataGrid you will see it creating
the ID value as you enter new records.

-------------------------
System.Data.DataTable dt = new DataTable("MyDataTable");

System.Data.DataColumn dc = new DataColumn("TableID");
dc.AutoIncrement = true;
dc.AutoIncrementSeed=1;
dc.AutoIncrementStep=1;
dt.Columns.Add(dc);

dc = new DataColumn("ValueField");
dt.Columns.Add(dc);
 
exactly what i needed. thank you Kirk.

Kirk Graves said:
Chuck,

depending on how you are using the DataTable, you probably don't even need
to worry about it. If you are writing back to the DB, the DataAdapter will
take care of this issue through it's UpdateCommand.

However, if you need to generate the unique number in the code, here is the
process for setting up a new DataTable that automaticaly increments an ID
column.

If you bind the resultant DataTable to a DataGrid you will see it creating
the ID value as you enter new records.

-------------------------
System.Data.DataTable dt = new DataTable("MyDataTable");

System.Data.DataColumn dc = new DataColumn("TableID");
dc.AutoIncrement = true;
dc.AutoIncrementSeed=1;
dc.AutoIncrementStep=1;
dt.Columns.Add(dc);

dc = new DataColumn("ValueField");
dt.Columns.Add(dc);
 
Back
Top