Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


ADO.NET: Problems when adding a new datarow to a datatable

Posted on 2005-03-16
Medium Priority
Last Modified: 2008-09-12

problem is the following:

I created a dataset and read a datatable from the database. If I want to add a new row to the
datatable I receive an error:

"Column ' name_of_pk ' does not permit zero value

Stack Trace:

at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos)
   at System.Data.DataTable.AddRow(DataRow row, Int32 proposedID)
   at System.Data.DataRowCollection.Add(DataRow row)

My code is the following:

because I use dataview's at the UI, I have to set the primary key for the datatable in order to find the selected columns from the dataview in the datatable.


myDataSet.Tables["tablename"].PrimaryKey =
new DataColumn[] {myDataSet.Tables["tablename"].Columns["name_of_pk"]};


DataRow theNewRow = myDataSet.Tables["tablename"].NewRow();
myDataSet.Tables["tablename"].Rows.Add( theNewRow );      <- the error occures here


The primary key in the database is set to autoincrement and I don't want to handle the primarykey in my application.
Is there a way to add the new Row to the DataTable without a value for the primary key?
Does anybody know a solution to that problem?

thanx in advance ;-)
Question by:new_user_xyz
LVL 18

Accepted Solution

testn earned 1000 total points
ID: 13554374
you should set the pk columnn to

yourpkcolumn.AutoIncrement = true;
yourpkcolumn.Seed = -1;
yourpkcolumn.Step = -1;
LVL 96

Expert Comment

by:Bob Learned
ID: 13554753
Are you setting any values for theNewRow?  It sure doesn't look like it.


Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

571 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