Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

handling db null values in C#

Posted on 2013-01-23
3
Medium Priority
?
407 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
[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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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: …

670 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