[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-09-21
9
Medium Priority
?
927 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 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