Solved

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

Posted on 2004-08-16
7
1,099 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Accepted Solution

by:
ryeandi earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 54
HttpPostedFile 1 29
How to Generate a Method from a keyboard shortcut in Visual Studio? 2 19
C# Linq - Join two objects into one 3 10
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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