Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please suggest me how to handle DBNUll values in foreach loop. Below is my code

C#
foreach (string  s in dr.Row.ItemArray)
{
    sb.Append(s);
    sb.Append("/t");
}


My item array have DBNull values. So I'm getting error.How to resolve this.

here at the time of getting value to string "s", I'm getting the error. not inside the loop...!
Posted
Updated 17-Nov-11 21:36pm
v4
Comments
LanFanNinja 18-Nov-11 0:55am    
Check solution below.
Pandya Anil 18-Nov-11 3:38am    
string is nullable type, you can assign null to it. it can not be error there, check once again :)

My Answer based on your Question details and comments in Solution-1.
Quote:
I can do the same what you suggested, If I'm able to enter inside the loop i can do like this but at the time of assigning value to string"s" I'm getting the error.

I doubt your Syntax is correct and event it is compiling without Error.

Have a look at below link for proper syntax of DataRow.ItemArray Property.

http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx

Property DataRow.ItemArray returns Array of object.

You may try by changing your code as below.
C#
DataRow dr = null;
StringBuilder sb = new StringBuilder();
//Assign some data to your dr here.
                
foreach (object o in dr.ItemArray)
{
  if (o != null && o != DBNull.Value)
  {
    sb.Append(Convert.ToString(o));
    sb.Append("/t");
  }
}
 
Share this answer
 
Well I don't mess with databases too much but something like this may help

C#
foreach (string s in dr.Row.ItemArray)
{
    if (s != System.DBNull.Value)
    {
        sb.Append(s);
        sb.Append("/t");
    }
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Nov-11 1:30am    
Well, I don't mess with databases too much but I'm pretty much sure it's correct as I recently did something like that; my 5. :-)
--SA
LanFanNinja 18-Nov-11 1:34am    
Thank you
avi_dadi2002 18-Nov-11 3:35am    
Thanks for your answer, I can do the same what you suggested, If I'm able to enter inside the loop i can do like this but at the time of assigning value to string"s" I'm getting the error.
LanFanNinja 18-Nov-11 8:50am    
Maybe use a for loop instead. i.e.
for (int i = 0; i < dr.Row.ItemArray.Length; i++)
{
if (dr.Row.ItemArray[i] != System.DBNull.Value)
{
}
}
I would do something like this

C#
foreach(object o in dr.Row.ItemArray)
{
    if(o!=DBNull)
    {
        sb.Append(o.ToString());
        sb.Append("/t");
    }
}


Your error is because there is no implicit conversion from DBNull to string. (I think!)
 
Share this answer
 
if you want to hadle only null

if(s != null))
{
sb.Append(s);
sb.Append("/t");
}

if you need to handle null and empty string

C#
if(!string.IsNullOrEmpty(s))
                {
                sb.Append(s);
                sb.Append("/t");
                }



but if you need to check ISDBNULL.. use this generic method.. or customize it in to your solution

C#
public static T GetDBValue<T>(SqlDataReader reader, string Column)
       {

           int _intColumnID = reader.GetOrdinal(Column);

           if (reader.IsDBNull(_intColumnID))
               return default(T);
           else
               return (T)reader.GetValue(_intColumnID);

       }



hope this will help...
 
Share this answer
 
C#
foreach (object o in dr.Row.ItemArray)
{
    if (o is string)
    {
        sb.Append(o.ToString());
        sb.Append("/t");
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900