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
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
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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