Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-16
7
Medium Priority
?
1,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:chmohan
ID: 11815224
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
ID: 11815511
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
ID: 11815589
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
ryeandi earned 2000 total points
ID: 11816019
//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
ID: 11818784
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
ID: 11820706
thanks ryeandi!! that was EXACTLY what I asked for and what I needed!
0
 
LVL 1

Expert Comment

by:ryeandi
ID: 11821262
glad to help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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