Solved

handling db null values in C#

Posted on 2013-01-23
3
397 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

19 Experts available now in Live!

Get 1:1 Help Now