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?
 
Use the following statement to generate the Unique id

System.Guid.NewGuid()

Regards,
Amal
 
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top