473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Retrieving image from database

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;

}
}
}
Apr 19 '08 #1
1 3018
tlhintoq
3,525 Expert 2GB
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

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;

}
}
}
Apr 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jari Dolberg | last post by:
Hi, I am looking for a solutions for the following problem, I need to access at database from within a website, from where I can retrive a specific document in the database and mailing it to...
1
by: Roman Kagan | last post by:
Hi everyone, I am successful in retrieving the image from the table (.TIF), however, the retrieved file does not work. It looks like an image file - extension, size and all, but the image is not...
6
by: Dean Slindee | last post by:
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like to see the statements that would move the...
0
by: kid4rilla | last post by:
I can successfully write the binary data to an image data type, and successfully retrieve it, but when I attempt to play the file in media player after retrieving it, I get the file type isn't...
0
by: islay | last post by:
I'm trying to retrieve an image from an Access 97 database and printing it on a report. I'm using a 'System.Data.OleDb.OleDbConnection()' object to connect to the database, a...
7
by: Sirplaya | last post by:
I am retrieving images that I stored in SQL Server on my web pages in C#. I have no problem with the images displaying, however, I am trying to wrap the image with an <A HREF ..." and each time I...
1
by: san123456789 | last post by:
Hi, Im using a MS SQL server to store images... Im creating a windows form application using C#. I need to store and retrieve images from the database. This is my code to store images: ...
0
mafaisal
by: mafaisal | last post by:
Hi Everybody I am Using Vb.net 2005 My Error when i Retrieve imge file from sql2005 database parameter not valid error is coming my code is Dim Logo As Byte() = RsC.Fields("Logo").Value
2
mafaisal
by: mafaisal | last post by:
Hi Everybody I am Using Vb.net 2005 My Error when i Retrieve imge file from sql2005 database my code is Dim Logo As Byte() = RsC.Fields("Logo").Value Dim stream As System.IO.MemoryStream
2
by: shivapadma | last post by:
i have inserted the image into database using the following code String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String dbName =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.