Connecting Tech Pros Worldwide Help | Site Map

Retrieving image from database

Newbie
 
Join Date: Apr 2008
Posts: 7
#1: Apr 19 '08
Pls Help me--------------
I use ASP(C#.NET) and oracle.
When i retrive image from database .it display error message...
System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
my code in the show button is.pls anybody help me ,how to retrive and display the image
protected void btnshow_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
MemoryStream stream = new MemoryStream();
foreach (DataRow dr in dt.Rows)
{ if (dr[0].ToString() == drpeno.SelectedItem.ToString())
{
byte[] blob = (byte[])dr[1];
stream.Write(blob,0,blob.Length);
Bitmap bitm = new Bitmap(stream);
Response.ContentType = "image/jpeg";
bitm.Save(Response.OutputStream,ImageFormat.Jpeg);
Label1.Text = dr[0].ToString();
// pcimage.ImageUrl = "~/showimage.ashx?id=" + id;

}
}
}
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,761
#2: Apr 20 '08

re: Retrieving image from database


I know nothing about databases, let me say that now.

But I think I can interpret the error message for you.
The error reads "InvalidCastException". So you are being told that you tried to cast something from one type to another and that cast failed.

Looking at your code I suspect the offending command was "Bitmap bitm = new Bitmap(stream);" The syntax Bitmap(stream) reads like you are trying to convert a stream object to a Bitmap object. Instead try " = new Bitmap.FromStream(stream);"

I usually am doing "Image.FromStream(xxx)", but I see that Bitmap inherits from Image and there is a FromStream member to Bitmap, so I'm guessing it should work.

If not, maybe this will help point you in a workable direction.
http://support.microsoft.com/kb/309482


tlhIn'toQ

Quote:

Originally Posted by sana krishna

Pls Help me--------------
I use ASP(C#.NET) and oracle.
When i retrive image from database .it display error message...
System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
my code in the show button is.pls anybody help me ,how to retrive and display the image
protected void btnshow_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
MemoryStream stream = new MemoryStream();
foreach (DataRow dr in dt.Rows)
{ if (dr[0].ToString() == drpeno.SelectedItem.ToString())
{
byte[] blob = (byte[])dr[1];
stream.Write(blob,0,blob.Length);
Bitmap bitm = new Bitmap(stream);
Response.ContentType = "image/jpeg";
bitm.Save(Response.OutputStream,ImageFormat.Jpeg);
Label1.Text = dr[0].ToString();
// pcimage.ImageUrl = "~/showimage.ashx?id=" + id;

}
}
}

Reply