Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I'm using mysql with c#. I'm storing image files into mysql with BLOB data-type.
But whenever I'm retrieving the value from this field, I want to check whether it's NULL or not. How can I do that?

Something like below code. It's not working.

C#
if ((ds3.Tables["tempEmpDetails"].Rows[0]["Emp_Photo"] != System.DBNull))
{
    Byte[] byteBLOBData = new Byte[0];
    byteBLOBData = (Byte[])(ds3.Tables["tempEmpDetails"].Rows[0]["Emp_Photo"]);
    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
    pictureBox2.Image = Image.FromStream(stmBLOBData);
}
else
{
    pictureBox2.Image = Properties.Resources.profile;
}
Posted
Updated 11-Mar-11 5:23am
v2
Comments
HimanshuJoshi 11-Mar-11 11:24am    
Updated to remove extra PRE tags.

1 solution

You can't test against System.DBNull - that's a class. Either use System.DBNull.Value, or try this:
C#
object o = ds3.Tables["tempEmpDetails"].Rows[0]["Emp_Photo"];
if (o is System.DBNull))
    {
    pictureBox2.Image = Properties.Resources.profile;
    }
else
    {
    Byte[] byteBLOBData = (Byte[])(o);
    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
    pictureBox2.Image = Image.FromStream(stmBLOBData);
    }
 
Share this answer
 
Comments
DEB4u 11-Mar-11 11:41am    
can't we write like this?
if (o== System.DBNull)
{
///
}
OriginalGriff 11-Mar-11 14:46pm    
No! System.DBNull is not a instance or a variable. It is a class. Would you expect "if (17 == int)" to work?
You can use System.DBNull.Value as I said, if you prefer.
if (o == System.DBNull.Value)
{
...
}
But I find the "is" test easier to read and move obvious.
DEB4u 11-Mar-11 17:42pm    
ok..That's working perfectly..Thanx very much
Sergey Alexandrovich Kryukov 11-Mar-11 21:39pm    
My 5. And OP needs to understand classes, variables, etc., before getting to databases.
--SA

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