Cannot insert the value NULL

  • Thread starter Fabio Negri Cicotti [MCP]
  • Start date
F

Fabio Negri Cicotti [MCP]

Hi All.

I'm trying to insert data into 2 tables (parent-child) using the ADO.NET's
SetParentRow method. The parent table has an identity column as primary key.
When I execute the code below I get the following message: "Cannot insert
the value NULL into column 'hdtID', table
'myinstance.mydb.Hotel_Details_Lang'; column does not allow nulls. INSERT
fails." Any idea?

PS: To create the DataAdapters, I'm using SqlCommandBuilder.

public void Insert_Hotel_Details(string hdtDescription) {
DataSet dsTemp = null;
try {
// Get the dataset with those two tables to use;
dsTemp = Get_Hotel_Details();

//Parent Table;
DataRow drwParent = dsTemp.Tables["Hotel_Details"].NewRow();
drwParent["hdtTemp"] = false;
dsTemp.Tables["Hotel_Details"].Rows.Add(drwParent);

//Child Table;
DataRow drwChild = dsTemp.Tables["Hotel_Details_Lang"].NewRow();
drwChild.SetParentRow(drwParent);
drwChild["lngID"] = 1;
drwChild["hdtDescription"] = hdtDescription;
dsTemp.Tables["Hotel_Details_Lang"].Rows.Add(drwChild);

daHotel_Details.Update(dsTemp);
daHotel_Details_Lang.Update(dsTemp);
}
catch (System.Exception EErr) {
throw new Exception(EErr.Message, EErr);
}
finally {
if (dsTemp != null) dsTemp.Dispose(); dsTemp = null;
}
}


Thanks in advance.
 
W

Wessel Troost

When I execute the code below I get the following message: "Cannot insert
the value NULL into column 'hdtID', table
'myinstance.mydb.Hotel_Details_Lang'; column does not allow nulls. INSERT
fails." Any idea?
The obvious explanation would be:
The column hdtID of the table Hotel_Details_Lang does not allow NULL
values.

Make sure it does have a value, like:
drwChild["hdtID"] = 1;

If the hdtID column is the primary key, it might be a good idea to make it
auto-increment. That way it will be filled with a unique number by
default.

Greetings,
Wessel
 
F

Fabio Negri Cicotti [MCP]

Sorry for my poor explanation.

As I said, the parent table has an identity primary key which is
automatically generated and I set on the child table the value for "hdtID"
using the dataset's method SetParentRow. Although when I create the dataset
I've defined the relation among them, it yet does not fetch the identity
value to pass to the child table. Any other idea?

The relation defined:

dsTemp.Relations.Add("Hotel_Details_X_Hotel_Details_Lang",
dsTemp.Tables["Hotel_Details"].Columns["hdtID"],
dsTemp.Tables["Hotel_Details_Lang"].Columns["hdtID"]);

dsTemp.Relations["Hotel_Details_X_Hotel_Details_Lang"].Nested = true;



Best regards.

Fabio Negri Cicotti
Software Engineer

Wessel Troost said:
When I execute the code below I get the following message: "Cannot insert
the value NULL into column 'hdtID', table
'myinstance.mydb.Hotel_Details_Lang'; column does not allow nulls. INSERT
fails." Any idea?
The obvious explanation would be:
The column hdtID of the table Hotel_Details_Lang does not allow NULL
values.

Make sure it does have a value, like:
drwChild["hdtID"] = 1;

If the hdtID column is the primary key, it might be a good idea to make it
auto-increment. That way it will be filled with a unique number by
default.

Greetings,
Wessel
 
W

Wessel Troost

I've defined the relation among them, it yet does not fetch the identity
value to pass to the child table. Any other idea?
Might have an idea: before you call Update() on the Hotel_Details DataSet,
the new row will not have an ID. So the framework couldn't give it an ID.

It would be logical if the ID would be passed on to the child row after
calling Update(), but it looks like that doesn't happen.

Does it help if you
- Call Update() on the parent row before adding the child row?
- Manually pass on the ID after calling Update() on the parent row?
(As in: drwChild["hdtID"] = drwParent["hdtID"];)

Just some ideas :)

Greetings,
Wessel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top