0

I have a DataSet in C# composed from three tables, with columns only, and I am trying to populate these tables by adding rows. This is my first table (destinationTable) The two columns that I am trying to add rows to are primary keys and cannot be null. However, when I try to add to them, I get this console error either on the first column or second:

ERRORE: Column 'CONFIG_NAME' does not allow nulls.

This is my code:

var newRow = destinationTable.NewRow();
newRow["APP_CODE"] = mAppCode;
destinationTable.Rows.Add(newRow);

var newRow2 = destinationTable.NewRow();
newRow2["CONFIG_NAME"] = "pippo";
destinationTable.Rows.Add(newRow2);

Same thing happens if I try to add to any other existing column. Same error. Any suggestions? Thanks.

I tried different suggestions from my coworkers but it seems impossible to populate. I tried creating a temporary empty DataSet and add those columns there instead, and then copy everything to this DataSet, but I get the same error.

EDIT:

The problem revolved on another issue related on the DataSet population. However, the answer provided by the users here are correct in context; only one row is needed.

2
  • You are adding a new row for every new column. Just initialize all the column values once and do destinationTable.Rows.Add(newRow) once. Commented Feb 12, 2024 at 14:30
  • Hello, thanks for the reply. Using your solution I now get this error: ERRORE: This row already belongs to another table. I did this: var newRow = destinationTable.NewRow(); newRow["APP_CODE"] = mAppCode; newRow["CONFIG_NAME"] = "pippo"; sourceTable.Rows.Add(newRow); Commented Feb 12, 2024 at 14:36

1 Answer 1

0

As @SoftwareDeveloper mentioned in his comment, you should initialize all column values once and then add the row to the desired table. The error you're facing in your comment is caused because you try to add the new row to a different DataTable:

var newRow = destinationTable.NewRow(); // <- newRow is created from destinationTable
newRow["APP_CODE"] = mAppCode;
newRow["CONFIG_NAME"] = "pippo";
sourceTable.Rows.Add(newRow); // <- you have to use the same DataTable here

When using Rows.Add() You have to add the new row to the same DataTable you used for the NewRow().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.