Solved

handling db null values in C#

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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