PC Review


Reply
Thread Tools Rate Thread

Adding a row with null columns to a typed dataset in ADO.NET 2.0

 
 
tmeynink@gmail.com
Guest
Posts: n/a
 
      21st Nov 2005
Hi,

In ADO.NET 2.0 if you have a table with foreign keys in it, when you
add a row using the strongly typed add row method you pass references
to the foreign rows rather than the foreign key value itself.

If any of these foreign key row references are null, I get an
exception.

The only way I have found to work around this is to use the following
for each column with a foreign key that can be null:

TypedDataSet.SomeTableRow myNewRow = ds.SomeTable.NewSomeTableRow ();
TypedDataSet.ForeignTableRow foreignRow = ds.ForeignTable.FindByID
(id);
//
// repeat for each foreign key column that allows nulls...
//
if (null != foreignRow)
{
myNewRow.ForeignKeyID = foreignRow.ID;
}
else
{
myNewRow["ForeignKeyID"] = DBNull.Value;
}

ds.SomeTable.AddSomeTableRow (myNewRow);

I must be missing something here - surely the generated code is smart
enough to detect null values for foreign key references and insert a
DBNull.Value in the appropriate foreign key columns (where null values
are allowed of course).

What am I missing here?

Thanks,
Todd

 
Reply With Quote
 
 
 
 
luxspes
Guest
Posts: n/a
 
      22nd Nov 2005
(E-Mail Removed) wrote:
> Hi,
>
> In ADO.NET 2.0 if you have a table with foreign keys in it, when you
> add a row using the strongly typed add row method you pass references
> to the foreign rows rather than the foreign key value itself.
>
> If any of these foreign key row references are null, I get an
> exception.
>
> The only way I have found to work around this is to use the following
> for each column with a foreign key that can be null:
>
> TypedDataSet.SomeTableRow myNewRow = ds.SomeTable.NewSomeTableRow ();
> TypedDataSet.ForeignTableRow foreignRow = ds.ForeignTable.FindByID
> (id);
> //
> // repeat for each foreign key column that allows nulls...
> //
> if (null != foreignRow)
> {
> myNewRow.ForeignKeyID = foreignRow.ID;
> }
> else
> {
> myNewRow["ForeignKeyID"] = DBNull.Value;
> }
>
> ds.SomeTable.AddSomeTableRow (myNewRow);
>
> I must be missing something here - surely the generated code is smart
> enough to detect null values for foreign key references and insert a
> DBNull.Value in the appropriate foreign key columns (where null values
> are allowed of course).

Are you sure that null ara allowed in the foreing keys you are testing
with (nulls might be allowed at the database level... but that does not
mean they are allowed at the dataset level).
I think you have to check if the "minOccurs" property for the datacolumn
is correctly set. (In this case if the foreign key is not mandatory,
minOccurs should be equal to zero)


>
> What am I missing here?
>
> Thanks,
> Todd
>

 
Reply With Quote
 
tmeynink@gmail.com
Guest
Posts: n/a
 
      22nd Nov 2005
I checked the "minOccurs" attribute and it was in fact set to zero for
the appropriate columns. The schema's generated by VS 2005 are very
different to those from VS 2003.

Any other ideas?

Thanks for your help.
Todd

 
Reply With Quote
 
Luxspes [MCP]
Guest
Posts: n/a
 
      23rd Nov 2005
(E-Mail Removed) wrote:
> I checked the "minOccurs" attribute and it was in fact set to zero for
> the appropriate columns. The schema's generated by VS 2005 are very
> different to those from VS 2003.
>
> Any other ideas?
>
> Thanks for your help.
> Todd
>



Can you copy the exact excepcion message and stack trace?
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify a field with a null value in a Strongly Typed DataSet michael Microsoft ADO .NET 2 17th Sep 2008 11:19 AM
Please Help...Typed dataset not saving datetime null value Ron Microsoft C# .NET 0 25th Mar 2008 07:23 PM
typed dataset + null values + cm.EndCurrentEdit Paul Microsoft ADO .NET 1 28th Nov 2005 12:01 PM
How to set strongly typed dataset attribute to null? =?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?= Microsoft ADO .NET 1 15th Jan 2005 12:52 AM
Typed Dataset and Null Values Elmer Miller Microsoft ADO .NET 2 23rd Nov 2003 08:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:18 AM.