Solved

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

Posted on 2004-08-16
7
1,104 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

691 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