468,780 Members | 2,383 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

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

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
Nov 16 '05 #1
0 1466

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Carlos San Miguel | last post: by
11 posts views Thread by Vani Murarka | last post: by
6 posts views Thread by Maurizio Faini | last post: by
2 posts views Thread by Danko Greiner | last post: by
10 posts views Thread by MaRCeLO PeReiRA | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.