Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai
i am store image in sql server as binary in sql server,my sql field datatype ia image,so can save binary format in that column.if not save any image(<binary data="">) in that column,if i see in sql server it show "NULL" word,but if i retrieve that column,its blank row.

my question is how to check that column have binary data or not,bcz

1.if i check row count,it will show blank row so count is 1 ---cant check count.
2.If i check "NULL" word,it will show "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'" -- cant check "NULL" word
3.if i check Is Nothing same error throws.

So how i check this image datatype field in vb.net using if condition ?


Pls reply asap

Regards
Aravind
Posted

C#
using System.Drawing;
if (dr["Photo"].ToString() != "")
{
    Byte[] image = (Byte[])(dr["Photo"]);
    string src = "data:image/jpeg;base64," + Convert.ToBase64String(image);
    picPhoto.ImageUrl = src;
}
else
{
 //if you want to set some default image
picPhoto.ImageUrl = "~/images/DefaultImg.png";
}

VB
VB
If dr("Photo").ToString() <> "" Then
    Dim image As [Byte]() = DirectCast(dr("Photo"), [Byte]())
    Dim src As String = "data:image/jpeg;base64," + Convert.ToBase64String(image)
    picPhoto.ImageUrl = src
Else
    picPhoto.ImageUrl = "~/images/DefaultImg.png"
End If
 
Share this answer
 
v2
Hi i solved this problem by followings codes,in my datatable if i get binary values then it not empty,if datatable get null value or blank row then that row id db null.


VB
Dim data = objdtr.Rows(0)("LogoImg")

If (TypeOf data Is DBNull) Then
   PictureBox6.Image = Global.Hospital.My.Resources.Resources.P123__half_size_
Else
'Here i download byte and convert into image and passed to PictureBox6 image
End IF


I will save png image in sql server as binary format,so only can retrieve bytes and convert into image again and use it in any place.some time that image column is empty.

i am select that column and fill in datatable and get that value in data,and i check it like type of data is dbnull or not,if it is null i will pass default image from resource folder,if not then convert byte and image and pass to picturebox.

Why i cant check directly data is null or not,or row count or data ="" (double quots),bcz it show blank row ,so cant count,if it is check "NULL" or "" it show error like "System.DBNull' to type 'System.Byte[]".


Regards
Aravind
 
Share this answer
 
Check if the byte array has length greater than 0.
 
Share this answer
 
Comments
Aravindba 7-Apr-14 6:54am    
Thank u for ur reply.i will get error like "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]"

Actually if i see in sql server all image rows are "NULL",and if i retrive in datatable,it have blank row,so cant count row also,bcz if blank row or System.Binay both count as 1.

How i check image column as null or nothing or have System.Binary Data ?

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