Solved

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

Posted on 2004-08-16
7
1,102 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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