Null and DBNull.value

N

Nathan Kovac

I have a field in a table that allows nulls. I used the wizard to create a dataset for this table. I now want to add a new row to the dataset and default in some of the values, but leave some of them null.
The Neither of the following lines work:

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1, 1, DBNull.Value, 1, DateTime.Today, DateTime.Today, 0, "");

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1, 1, null, 1, DateTime.Today, DateTime.Today, 0, "");

I always get an error saying can't convert DBNull.Value to data type int or cannot convert null to datatype int. In the xsd which defines the datatype for that element I see this line. Do I need to add something to allow it to be null?

<xs:element name="SubprojectID" type="xs:int" minOccurs="0" />

Your help is appreciated.



Thanks,

Nathan
 
G

Guest

It may be kludgey, but generally do the following

Create a new row from the table
Populate only the fields in the row that are non-null
Add the new row to the table

If your schema is setup to allow null columns, then a NewRow() created by the table will have these fields in the ItemArray initialized to null/DBNull appropriately

-- tbrow


----- Nathan Kovac wrote: ----

I have a field in a table that allows nulls. I used the wizard to create a dataset for this table. I now want to add a new row to the dataset and default in some of the values, but leave some of them null
The Neither of the following lines work

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1, 1, DBNull.Value, 1, DateTime.Today, DateTime.Today, 0, "")

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1, 1, null, 1, DateTime.Today, DateTime.Today, 0, "")

I always get an error saying can't convert DBNull.Value to data type int or cannot convert null to datatype int. In the xsd which defines the datatype for that element I see this line. Do I need to add something to allow it to be null

<xs:element name="SubprojectID" type="xs:int" minOccurs="0" /

Your help is appreciated



Thanks

Natha
 
N

Nathan Kovac

Thanks

tbrown said:
It may be kludgey, but generally do the following:

Create a new row from the table.
Populate only the fields in the row that are non-null.
Add the new row to the table.

If your schema is setup to allow null columns, then a NewRow() created by
the table will have these fields in the ItemArray initialized to null/DBNull
appropriately.
-- tbrown


----- Nathan Kovac wrote: -----

I have a field in a table that allows nulls. I used the wizard to
create a dataset for this table. I now want to add a new row to the dataset
and default in some of the values, but leave some of them null.
The Neither of the following lines work:

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1,
1, DBNull.Value, 1, DateTime.Today, DateTime.Today, 0, "");
ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1,
1, null, 1, DateTime.Today, DateTime.Today, 0, "");
I always get an error saying can't convert DBNull.Value to data type
int or cannot convert null to datatype int. In the xsd which defines the
datatype for that element I see this line. Do I need to add something to
allow it to be null?
 
M

Manish Jain

replace DBNull.Value with (object)DBNull.Value.

Manish

I have a field in a table that allows nulls. I used the wizard to create a dataset for this table. I now want to add a new row to the dataset and default in some of the values, but leave some of them null.
The Neither of the following lines work:

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1, 1, DBNull.Value, 1, DateTime.Today, DateTime.Today, 0, "");

ticketDataSet1.Ticket.AddTicketRow(1, Session.Instance.employeeID, 1, 1, null, 1, DateTime.Today, DateTime.Today, 0, "");

I always get an error saying can't convert DBNull.Value to data type int or cannot convert null to datatype int. In the xsd which defines the datatype for that element I see this line. Do I need to add something to allow it to be null?

<xs:element name="SubprojectID" type="xs:int" minOccurs="0" />

Your help is appreciated.



Thanks,

Nathan
 

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