473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1669

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
4
by: Carlos San Miguel | last post by:
Hi, I'm going to be using Oracle with a customer. I will access Oracle with ODBC. All the tables have an Identity column, this is the ID for the record. I need to know how I can get the value of...
11
by: Vani Murarka | last post by:
Hi Everyone, Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for...
6
by: Maurizio Faini | last post by:
I have a little problem. there is a way to get last id inserted into db or i have to make a new query? I explain better my question: in vbscript using sqlserver2000 i can use this code: ...
2
by: Danko Greiner | last post by:
I'm trying to update every table in DataSet, but getting inserted "id_user" gets me an exception: column "id_user" is read only if(_dataSet.HasChanges()) { SqlDataAdapter _adapter;...
10
by: MaRCeLO PeReiRA | last post by:
Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence...
3
by: shapper | last post by:
Hello, I am created an Asp.Net 2.0 repeater implementing the ITemplate class. I know my datasource, a datatable has 6 rows, but the repeater only displays the last one. If I add more rows...
2
by: imranabdulaziz | last post by:
Dear all, i am using asp.net ,C# (VS 2005) and sql server 2005. i have written sp for inserting the the data which written last inserted idendity no. i would like to which method should i...
0
by: Stardust | last post by:
Hello everyone! I am new to VB.NET so please accept my apologies if the question is too easy. I created a concise code to explain the problem I have. I have an Access database that contains...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.