string strmcat = "select idcategory from categories where categorydesc='" + mcategory + "'";
SqlCommand com = new SqlCommand(strmcat, conn);
conn.Open();
Object o = (Object)com.ExecuteScalar();
if ( o != DBNull.Value)
{
idmcat = (int)com.ExecuteScalar();
}
conn.Close();
What wrong with this code.When null values return from db then also control goes in if condition.
Loading
Suthish NairPosted Feb 17, 2011, 3:30 PM
ExecuteScalar returns null if no value is returned, and DBNull.Value if a null is returned.
Pankaj,
So try like this..
Amit ChoudharyPosted Feb 17, 2011, 1:29 AM
Thanks.
Syed ShakeerPosted Feb 17, 2011, 12:35 AM
You can check the object if it is null or not as follows:
Object o = (Object)com.ExecuteScalar();
if ( o != Null)
{
}