Solved

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

Posted on 2004-09-21
9
841 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now