By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

Cannot insert the value NULL

P: n/a
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.

--
Fabio Negri Cicotti
Software Engineer
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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
Nov 17 '05 #2

P: n/a
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_Detail s_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" <no*****@like.the.sun> wrote in message
news:op.suifpk0bf3yrl7@asbel...
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

Nov 17 '05 #3

P: n/a
> 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
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.