user249375
user249375

Reputation:

using repeater control to display an image from database C#

I have a book database(2 books in total right now) where for each book i have a row called 'Image' that contains the name of the images. For example. cprog.jpeg and asm.jpeg

Using the repeater i can display the book information like name, author, etc. But i dont know how to get the images to display. The images are stored in the images folder.

here is the aspx which is the issue? since, it displays the bookname and description just fine.

<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
 <%# DataBinder.Eval(Container.DataItem, "BookName") %>
 <hr>
 <%# DataBinder.Eval(Container.DataItem, "BookDescription") %>
 <td width="100px">
  <p align="left">


 <img src= '<%# DataBinder.Eval(Container.DataItem, "Image") %>.jpeg'  
 alt="" style="height:200px;width:200px;border:1px solid gray;"/>

  </td>
  </p>


Code behind

String connectionString = "Data Source=" + Server.MapPath(@"~\App_Data\bookDB.db");
        String selectCommand = String.Format("Select * from Book where CategoryName = '{0}'", Request.QueryString);
        SQLiteConnection myConnection = new SQLiteConnection();
        myConnection.ConnectionString = connectionString;
        myConnection.Open();
        SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(selectCommand, myConnection);

        myConnection.Close();
        // DataSet ds = new DataSet();
        dataAdapter.Fill(ds2);


        DataTable table = new DataTable();
        dataAdapter.Fill(table);
        Repeater1.DataSource = table;
        Repeater1.DataBind();

Once, when i ran the project the broken image appeared for a second and then disappeared. I have spent the last 2 days trying to get this with no avail... Thank you.

Upvotes: 1

Views: 27733

Answers (3)

Kalu Singh Rao
Kalu Singh Rao

Reputation: 1697

You can use this code to load bitmap image on asp.net page.

<img src="data:image/jpeg;base64,<%# ((string)Container.DataItem) %>" 
width="100" height="100"/>

Upvotes: 0

marapet
marapet

Reputation: 56516

Assuming the images folder is /images, adding the folder name and deleting the extension (already in database as mentioned in your question) should display the image:

<img src='/images/<%# DataBinder.Eval(Container.DataItem, "Image") %>'  
alt="" style="height:200px;width:200px;border:1px solid gray;"/>

If that doesn't yield the desired result, check the html output and compare it to the real url - what's different?

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160952

You have to put in the path to your image folder so the web server knows where to serve your image from:

<img src= 'images/<%# DataBinder.Eval(Container.DataItem, "Image") %>'  
 alt="" style="height:200px;width:200px;border:1px solid gray;"/>

Also since your image name in the database already contains the extension you have to remove that from the repeater binding.

Based on your example above would resolve to i.e:

<img src= 'images/cprog.jpeg' alt="" style="..."/>

Upvotes: 0

Related Questions