Solved

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

Posted on 2004-09-21
9
854 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 63
Syntax error 9 45
Remove greater than sign 3 46
C# HTTP GET method sample code 3 41
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

15 Experts available now in Live!

Get 1:1 Help Now