Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

handling db null values in C#

Posted on 2013-01-23
3
Medium Priority
?
408 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 501 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 498 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 501 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Integration Management Part 2
Loops Section Overview

876 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