How to get get the last inserted key value from DataTable using C#

R

rodrigo

How to get get the last inserted key (value NOT column) in a specific
dataTable dt right after dt.Rows.InsertAt according to code and schema
below. The key is a autoincrement column.

foreach (MyContainer item in al)


{


dr = dt.NewRow();


dr["item_id"] = item.item_id;


dr["description"] = item.description;


dr["price"] = item.price;


dr["qty"] = item.qty;


// INSERTS ROW in DataTable


dt.Rows.InsertAt(dr,Convert.ToInt32(RowIndex.Text)-1);


//GET KEY RIGHT HERE





}





The schema :


private DataTable DataGrid_GetColumnsSchema()


{


DataTable dtquote = new DataTable("quote");


DataColumn dc = new DataColumn();


dc = new DataColumn("key", System.Type.GetType("System.Int32"));


dc.AutoIncrement=true;


dc.AutoIncrementSeed=1;


dtquote.Columns.Add(dc);


dc = new DataColumn("item_id",
System.Type.GetType("System.String"));


dtquote.Columns.Add(dc);


dc = new DataColumn("description",
System.Type.GetType("System.String"));


dtquote.Columns.Add(dc);





dc = new DataColumn("price",
System.Type.GetType("System.Decimal"));


dtquote.Columns.Add(dc);





dc = new DataColumn("qty", System.Type.GetType("System.Int32"));


dtquote.Columns.Add(dc);





dc = new DataColumn("extended_price",
System.Type.GetType("System.Decimal"));


dtquote.Columns.Add(dc);





dc = new DataColumn("sortorder",
System.Type.GetType("System.Int32"));


dtquote.Columns.Add(dc);





DataColumn[] keys = new DataColumn[1];


keys[0] = dtquote.Columns[0];


dtquote.PrimaryKey = keys;





return dtquote;


}







Thank you Very much

Rod
 

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

Top