Solved

handling db null values in C#

Posted on 2013-01-23
3
400 Views
Last Modified: 2013-01-23
Hi

I have a table in sql server 2008 returning 13 rows but only one column(workcenter x13) has data the rest are all nulls.

i have this snippet of code where im assigning the db values to an object properties but it is failing because of the null values, is there a way i can handle assigning nulls?...

            DataView dv = ds.Tables[0].DefaultView;

            if (dv != null && dv.Table.Rows.Count > 0)
            {
                foreach (DataRowView dr in dv)
                {
                    var data = new Tracker.Web.Classes.GeneralUserTools.GeneralUserTools
                    {
                        WorkCentre = dr["WorkCenter"].ToString(), //this has data
                        WCStatus = dr["WCStatus"].ToString(),
                        PassQty = Convert.ToInt32(dr["Pass"].ToString()),
                        FailQty = Convert.ToInt32(dr["Fail"].ToString()),
                        Operator = dr["Operator"].ToString(),
                        Date = Convert.ToDateTime(dr["WCDate"].ToString()),
                    };
                    lot.Add(data);
                }
            }
0
Comment
Question by:razza_b
3 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 167 total points
ID: 38809334
you need to compare the returned value to DBNull.Value
eg.
if (dr["WCStatus"] != DBnull.Value)
{
    WCStatus = dr["WCStatus"].ToString();
}
else
{
    WCStatus = string.Empty;
}
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 166 total points
ID: 38809361
if you have columns that have NULL values, you either:
* modify the SQL to make sure it won't return NULL values, but some other value instead
* check each value before using it if it's NULL
pseudo-code
if ( dr["yourfield"].Value == DBNull.Value  )  {  do this } else { do that }

Open in new window

0
 
LVL 10

Accepted Solution

by:
Monica P earned 167 total points
ID: 38809368
Can try something like this

before assigning a value to the Variables


 If Not IsDBNull(dr("WorkCenter")) Then

WorkCentre = dr["WorkCenter"].ToString(), //this has data
else
WorkCentre = ""// set default value

endif

Open in new window



or

You can declare a value type variable as nullable using the ? type modifier.

//This is the standard way to handle null values
double? BranchCode;
BranchCode = dr["BranchCode"];

this will not throw exception ..but u need to validate the values during calculation or assignment somewhere in code
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
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…
A short film showing how OnPage and Connectwise integration works.

932 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

17 Experts available now in Live!

Get 1:1 Help Now