Click to See Complete Forum and Search --> : dbnull.value if clause always reports null


Zolix2010
November 21st, 2004, 04:46 AM
hi, i got this code.


ArrayList list = new ArrayList();

for (int i=0; i < ds.Tables[0].Rows.Count; i++)

{

Tree t = new Tree();

t.Id = (double)ds.Tables[0].Rows[i][0];



if (ds.Tables[0].Rows[i][1] != DBNull.Value)

{

t.Owner = (double)ds.Tables[0].Rows[i][1];

}

else

{

t.Owner = 0;

}



if (ds.Tables[0].Rows[i][6] != DBNull.Value)

{

t.Screenshot = (string)ds.Tables[0].Rows[i][6];

}

else

{

t.Screenshot = "";

}



if (ds.Tables[0].Rows[i][7] != DBNull.Value)

{

t.LeafScreenshot = (string)ds.Tables[0].Rows[i][7]; }

else

{

t.LeafScreenshot = "";

}

list.Add(t);

}




the loop enters the details to Tree obj and then to the list collection.
the problem is that when i got the first null value from ds (dataset),
the Id value is continues to be entered OK but all the other fields (screenshot, owner etc) isn't.
i mean that after the first time of null in the ds , the if clause will always report null.
does anyone got a clue why?

Krzemo
November 21st, 2004, 01:02 PM
I don't believe that it should be working ...

DataRow contains Null values in specific data type. For example Varchar NULL column will be returned as System.Data.SqlTypes.SqlString.Null (not DBNull.Value)

What it will do 4 your code explains some excersises in immediate window:

?System.Data.SqlTypes.SqlString.Null==DBNull.Value
false

?System.Data.SqlTypes.SqlString.Null!=DBNull.Value

true


IMHO U shuld rather use if (!ds.Tables[0].Rows[i].IsNull(1)) syntax instead.

Best regards,
Krzemo.

Zolix2010
November 21st, 2004, 05:32 PM
welp,
i put IFNULL in the sql query and it solved it.
thanks for your time man.