Solved

how to insert new DataSet/DataTable row that has a primary key assigned

Posted on 2004-08-16
7
1,091 Views
Last Modified: 2012-05-05
i need to insert a new row in my dataset datatable. my datatable has a primary key. i need to assign a unique value for my primary key column in my newly inserted row. how can i do this? i need to figure out how to find the max value of primary key in my dataset and add a 1 to it to have an unused unique value.

tks!
0
Comment
Question by:trevoray
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:chmohan
Comment Utility
when u get the datatable use the sql  query to sort by the primary key column ASC
That gives u the max number

then  DataRow dr =       YourDataTable.NewRow();
int iCount = YourDataTable.Rows.Count;
int iPRimaryID = YourDataTable.Rows[iCount]["PrimaryID Col name"];

 iPRimaryID = iPRimaryID +1;
 dr [""PrimaryID Col name"]= iPRimaryID ;
 then add all the other column values and in the end
YourDataTable.Rows.Add(dr);

0
 

Author Comment

by:trevoray
Comment Utility
hey thanks, but i don't think that will work. i will constantly be adding and deleting rows on my 'internal, disconnected' datatable. so, i need to be able to look for the highest number for the disconnected data, OR find a way that automatically generates a unique value for me for that field. thanks!
0
 
LVL 1

Expert Comment

by:chmohan
Comment Utility
DataView myDataView =YourDataTable.DefaultView;
   // By default, the first column sorted ascending.
   myDataView.Sort = "PrimaryID Col DESC";
then see this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadataviewclasstopic.asp
 



use    the DataView object to sort your disconnected recordset
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Accepted Solution

by:
ryeandi earned 500 total points
Comment Utility
//get the highest primary key value in the datatable.
int MaxPKey = Convert.ToInt16(DS.Tables["tableName"].Compute("MAX(PK)","PK <> -1"));

//create a new row of the same format as the datatable's other rows
DataRow dr = DS.Tables["tableName"].NewRow();

//add the new primary key
dr["PK"] = MaxPKey + 1;

//add the rest of the row associated with this pk
dr["otherRow"] = otherColValue

//add the new row to the table
DS.Tables["tableName"].Rows.Add(dr);
0
 
LVL 7

Expert Comment

by:illusio
Comment Utility
Hi,

I think there is a very easy solution to this problem... Just take a look at the following code:

Creating the datatable:
      DataTable dt = new DataTable("Unique");
      dt.Columns.Add(new DataColumn("ID", typeof(int)));
      dt.Columns.Add(new DataColumn("Data", typeof(string)));
      dt.Constraints.Add("PKey", dt.Columns["ID"], true);
      dt.Columns["ID"].AutoIncrement = true;
      dt.Columns["ID"].AutoIncrementSeed = 1;
      dt.Columns["ID"].AutoIncrementStep = 1;

Adding a new row with unique, autoincremented id:
      DataRow dr = dt.NewRow();
      dr["Data"] = "string - " + i.ToString();
      dt.Rows.Add(dr);

You can play with the AutoIncrementSeed if you continue to use an existing table (setting it to the MAX + 1 value after having filled the table with existing values)

Regards,
Peter
0
 

Author Comment

by:trevoray
Comment Utility
thanks ryeandi!! that was EXACTLY what I asked for and what I needed!
0
 
LVL 1

Expert Comment

by:ryeandi
Comment Utility
glad to help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now