Link to home
Start Free TrialLog in
Avatar of new_user_xyz
new_user_xyz

asked on

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

Hello,

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 ;-)
ASKER CERTIFIED SOLUTION
Avatar of testn
testn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
Are you setting any values for theNewRow?  It sure doesn't look like it.

Bob