?
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
?
911 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

743 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