473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving image from database

7 New Member
Pls Help me--------------
I use ASP(C#.NET) and oracle.
When i retrive image from database .it display error message...
System.InvalidC astException: 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(o bject sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
MemoryStream stream = new MemoryStream();
foreach (DataRow dr in dt.Rows)
{ if (dr[0].ToString() == drpeno.Selected Item.ToString() )
{
byte[] blob = (byte[])dr[1];
stream.Write(bl ob,0,blob.Lengt h);
Bitmap bitm = new Bitmap(stream);
Response.Conten tType = "image/jpeg";
bitm.Save(Respo nse.OutputStrea m,ImageFormat.J peg);
Label1.Text = dr[0].ToString();
// pcimage.ImageUr l = "~/showimage.ashx? id=" + id;

}
}
}
Apr 19 '08 #1
1 3065
tlhintoq
3,525 Recognized Expert Specialist
I know nothing about databases, let me say that now.

But I think I can interpret the error message for you.
The error reads "InvalidCastExc eption". 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.FromStre am(stream);"

I usually am doing "Image.FromStre am(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.microso ft.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.InvalidC astException: 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(o bject sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
MemoryStream stream = new MemoryStream();
foreach (DataRow dr in dt.Rows)
{ if (dr[0].ToString() == drpeno.Selected Item.ToString() )
{
byte[] blob = (byte[])dr[1];
stream.Write(bl ob,0,blob.Lengt h);
Bitmap bitm = new Bitmap(stream);
Response.Conten tType = "image/jpeg";
bitm.Save(Respo nse.OutputStrea m,ImageFormat.J peg);
Label1.Text = dr[0].ToString();
// pcimage.ImageUr l = "~/showimage.ashx? id=" + id;

}
}
}
Apr 20 '08 #2

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

Similar topics

3
1470
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 someone else. Of course I cut crab the document from the database and save it on my harddrive, and then send it. But this process don't fit into our busy days. I thought that javascript might be an idea?
1
1992
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 visible. Any ideas? I'm including the first few lines (image opened with Notepad) of the file below. Please, help. """""
6
16114
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 Image returned in the dataset to a picture box, like: For Each dr in ds.tables(0).rows Pic.Image = dr("ImageColumnName") 'not this simple! Next Thanks,
0
1247
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 supported. I must be losing something somewhere, but the byte lengths match up. Is there some encoding I need to do. Please take a look, and suggestions would be appreciated.
0
966
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 OleDb.OleDbCommand(queryString, dbconn) object to querry, and a OleDb.OleDbDataReader object to read the image. The read is working but it's returning a system.array object and I don't know how to convert to an image. -- Islay
7
2052
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 try, it acts like the link is not even on the image. What is the proper way to do this? Thanks.
1
2435
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: pictureBox.Image = new System.Drawing.Bitmap(this.openFileDialog.FileName);
0
1213
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
1860
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
2234
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 = "hibernatetutorial"; String userName = "root"; String password = "root";
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.