Link to home
Start Free TrialLog in
Avatar of gdbjohnson
gdbjohnson

asked on

Is it possible to Assign DBNull to a column in a row in a Typed-DataSet?

I have a Strong-Typed DataSet, and even though a column can accept a null in my SQL Database, I am having a bit of trouble finding how I might assign a column to DBNull in code, rather than some default value.

I almost want to override my
<DataSet>.<Table>.Add<Table>Row()
method with one that excludes a column, or allows me to enter a DBNull for it, instead of, say, an int.

Is it possible?

g
Avatar of tomasX2
tomasX2

The code generated should include functions to set them to null
for instance...
SetPhoneNumberNull();

unless you have declared the column so that it cannot be null

They also have IsPhoneNumberNull();
this would be on a datarow object.
The problem is that a int column cannot have a dbnull value. What u can do is instead of sending null values from the database for this column send 0
Or write a function

public int IntValue(Object 0)
{
  if( (o == null) || (o==dbnull.value))
{
return Convert.ToInt32(null);
}
else
{
return Convert.ToInt32(o);
}
}

Before u databind the datagrid
for(int i = 0;i<=rowcount;i++)
{
datatable.Rows(i)(intColumn) = IntValue(datatable.Rows(i)(intColumn));  //Pass this column value through the IntValue function
}
An int column can have a dbnull value and you should be able to call the SetMyIntValueNull() function.
Avatar of gdbjohnson

ASKER

but what about the initial row creation part.  I know how to test for DBNull, but my problem is when I first create a new row in the dataset:

the method is:
ds.MyTable.AddMyTableRow(int col1, int col2, int col3)

My problem is that I don't have a value to set for col3.  How do I add a row to the dataset so that col3 == DBNull.

Are you saying to assign it an int initially, and then set it to DBNull after the row is added?

-g
ASKER CERTIFIED SOLUTION
Avatar of tomasX2
tomasX2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
haha..  ok.  I knew it was something easy.

g
glad to help... good luck