Solved

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

Posted on 2004-09-21
9
887 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:gdbjohnson
  • 5
  • 2
  • 2
9 Comments
 
LVL 5

Expert Comment

by:tomasX2
ID: 12117639
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();
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12118318
this would be on a datarow object.
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12120370
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:123654789987
ID: 12120403
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
}
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12121061
An int column can have a dbnull value and you should be able to call the SetMyIntValueNull() function.
0
 
LVL 2

Author Comment

by:gdbjohnson
ID: 12123373
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
0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 75 total points
ID: 12123395
DataRow row = ds.MyTable.NewMyTableRow();
row.col1 = "value";
row.col2 = "value2";
ds.MyTable.AddMyTableRow(row);
0
 
LVL 2

Author Comment

by:gdbjohnson
ID: 12123498
haha..  ok.  I knew it was something easy.

g
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12123506
glad to help... good luck
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question