Community discussion forum
Retrieving Images from a Database
-
This thread is for discussions of Retrieving Images from a Database.
-
Advertisement
Simply the fastest line-level profiler for .NET ever
“The low overhead means it has minimal impact on the execution of my program”
Mark Everest, Development Team Leader, Renault F1 Team Ltd.Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now
-
Hi Dave
I was going thru your article on uploading and retreiving image from database. It was really good. My requirement is slightly different. When i retreive the image, i would like to display the image on the Image control in asp.net. How do i do that.
Regards
Ajay. -
Yes, useful as far as it goes, but I would have hoped for:
a. more information on contentypes
b. a more complete discussion of the output options, for example displaying it in an image control, resizing it, etc.
Perhaps a part II?
John B -
Hi
I have a program code in java which is able to retrive and store information in text in a Access database but need the program to be able to do same for Images. Please can anyone help.
Code Below
import java.awt.image.;
import java.awt.;
import java.sql.;
import java.util.;
import javax.swing.*;
public class Histogram extends JFrame {
//JDBc driver name and database URL
static final String driver= "sun.jdbc.odbc.JdbcOdbcDriver";
static final String url="jdbc
dbc:Histogram Database_1";
//declare Connection and Statement for accessing and querying database
private Connection connection;
private Statement statement;
//Constructor connects to database, quries database, processes
//results and display results in window
public Histogram()
{
super("Picture Table of Histogram Database_1");
//connect to database Histogram and query database
try{
//load database driver class
Class.forName(driver);
//establish connection to database
connection = DriverManager.getConnection(url);
//create Statement for querying database
statement = connection.createStatement();
//query database
ResultSet resultSet =
statement.executeQuery("SELECT Filename FROM PictureTable");
/////////////$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//String CurrentFile = NULL;
//Image image1.open(CurrentFile);
//CurrentFile = statement.executeQuery("SELECT Filename FROM PictureTable WHERE PictureID=1");
////////////$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//process query result
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
for(int i = 1; i <=numberOfColumns; i++ )
results.append( metaData.getColumnName(i) + "\t");
results.append("\n");
while( resultSet.next()){
for(int i = 1; i <=numberOfColumns; i++)
results.append( resultSet.getObject(i) + "\t");
results.append("\n");
}
//set up GUI and display window
JTextArea textArea = new JTextArea( results.toString() );
Container container = getContentPane();
container.add( new JScrollPane( textArea ) );
setSize(300, 100 );
setVisible(true);
}
//detect problem interacting with the database
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog ( null, sqlException.getMessage(),
"Database Error", JOptionPane.ERROR_MESSAGE );
System.exit(1);
}
//detect problem loading database driver
catch ( ClassNotFoundException classNotFound ){
JOptionPane.showMessageDialog ( null, classNotFound.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE );
System.exit(1);
}
//ensure statement and connection are closed properly
finally{
try{
statement.close();
connection.close();
}
//handle exception closing statement and connection
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null, sqlException.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE );
System.exit(1);
}
}
}//end Histogram Database_1
//Lunch the application
public static void main( String args[] )
{
//error found here
Histogram window = new Histogram();
window.setDefaultCloseOperation( JFrame.EXITONCLOSE);
}
}//end class Histogram Database_1
-
4 years agoby
spam@metricmedia.com
spam@metricmedia.com
Seattle, United StatesJoined 4 years agoHow do you deal with Netscape 4 and other browsers that don't like showing images with .aspx extensions? Is there a way to let these browsers know that that the content is infact a JPG? I get this message:
Opening:
image.aspx using netscape.exe.
When you download a file from the network, you should be aware of security considerations.
A file that contains malicious programming instructions could damage or otherwise compromise the contents of your computer.
You should only use files obtained from sites that you trust.
What do you want to do with this file?
Open it
Save it to disk -
Hi, did you find a way to make it work they way you wanted?
I am looking for a way to put it inside a controll as well. -
hey guys i have the exact same problem as ye have and im just wondering if ye managed to find a solution
thanks in advance
tadhg88 -
Hi there! Try this „quite” simple fix. Thanks Dave, you helped a lot with this images mystery…
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="imgpk" DataSourceID="ImagesSqlDataSource" Width="627px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="imgpk" HeaderText="imgpk" InsertVisible="False" ReadOnly="True"
SortExpression="imgpk" />
<asp:BoundField DataField="imgname" HeaderText="imgname" SortExpression="imgname" />
<asp:BoundField DataField="imgcontenttype" HeaderText="imgcontenttype" SortExpression="imgcontenttype" />
<asp:TemplateField HeaderText="imgdata" SortExpression="imgdata">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("img_pk", "ViewImage.aspx?img={0:d}") %>'></asp:Image>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text="Sorry, it can not be edited"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> -
I've been struggling to get an image to view on my webpage. I had to make a few changes but no way would it work for me. I ended up coping and pasting everything, ran into the error at this point : SqlDataReader dr = command.ExecuteReader(); which I thought there was a problem in doing a search based on an index number being an int value and not string. I made the changes and the error went away but no image came out.
any help would be greatly appreciated.
Judy -
Whar is Bind? I've only seen DataBinder.Eval used here and your examp,le causes a Compilation Error.
-
I am Facing the sam e problem, i am able to store an image into a database as image type but fetching from database in displaying it on a web page is not working.
Fetching from the database is working fine it is not dispalying on a web page.
can any body help me out from this problem.
thanks
Manoj
Email me at : manoj.it01@gmail.com
-
There is a free out-of-the-box control that does this with less effort http://www.sharppieces.com/demo/databaseimage.aspx .
-
1 month agoby
kruelintent
kruelintent
Gloucester, England., United KingdomJoined 5 years agoI made a video showing how to do this a while back. Hope it helps.
http://tech.kruelintent.com/player.aspx?vid=8
Thanks Tom.
Post a reply
Quick links
Recent activity
- jack semwal replied to i have struck with my proj...
- Mandy Bradley replied to Help please for student
- Barry Roberts replied to Loop help needed
- Navin Hirani replied to Error in VB code
- Marissa Levy replied to Gizmox Announces release of...
- manjunath L replied to Regarding Visual Basic Programme
Enter your message below
Sign in or Join us (it's free).