Solved

Image from sql database to gridview

Posted on 2011-02-21
6
426 Views
Last Modified: 2012-05-11
Hi, experts.

I have a ms server 2008, and code my site in aspx C#.
I want to retrieve an image from my database, and i know it is corectly placed inside my db, because i can view the images in a test query.
I retrieve all my data to my GridView through a SQLDataSource.

I got 5 columns, but it is only the picture that won't show.

i tried a lot of solution in the gridview:

Boundfields, Templatefields and Imagefield.
and i tried to make my column a varbinary(max) and image.

I have even tried to make a picture in the string with ASCII:

SELECT '<asp:image runat="server" ImageURL="aspnet_Users.ComLogo" />' 'ComLogo' ,

I think the problem lies in the way i point to the data in my sql selection string, because when i open my site and view the source code, the html shows <img src="" />.

My last try was


            <asp:TemplateField HeaderText="Wild logo">
                <EditItemTemplate>
                </EditItemTemplate>
                <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<% #bind("ComLogo") %>' />
                </ItemTemplate>
             </asp:TemplateField>

Any ideas?
0
Comment
Question by:bskyum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34942474
A complete article about this

Inserting Images into Database and Display it in GridView through Handler.ashx
http://www.aspdotnetcodes.com/Insert_Images_Database.aspx

How to Display Image in GridView Control Using Property
http://www.devasp.net/net/articles/display/692.html

How to Display image in gridview from Database sqlserver
http://www.dotnetspider.com/forum/269973-How-Display-image-gridview-from-Database.aspx
0
 

Author Comment

by:bskyum
ID: 34942722
Great links.

But unfortunately none of the guides show how to write the imageurl...
0
 
LVL 2

Expert Comment

by:bapcai
ID: 34950358
It's my code. It run after binding data to grid.
 
private void DrawImage()
        {

            for (int i = 0; i < dgd_danhmuc.Items.Count; i++)
            {
                System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)dgd_danhmuc.Items[i].Cells[0].Controls[0].FindControl("Image1");
                //Template Column with System.Web.UI.WebControls.Image 
                if (dgd_danhmuc.Items[i].Cells[7].Text == "1") //dgd_danhmuc.Items[i].Cells[7].Text=="1"  hidden field
                {
                    ckb.ImageUrl = "../Image/lock.gif";
                }
                else
                {
                    ckb.ImageUrl = "../Image/unlock.gif";
                }

            }
        }

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bskyum
ID: 34950397
how dows it work??

and can i get the image into the gridview as one column, and be in the same record as the data that is in the same record in the database`?
0
 

Author Comment

by:bskyum
ID: 34950450
I mean, is dgd_danhmuc the id of a datagrid?

and what is ckb?
0
 
LVL 2

Accepted Solution

by:
bapcai earned 500 total points
ID: 34958118
Sorry for my comment. dgd_danhmuc is the id of a datagrid.
 'ckb' -> You replace 'ckb' with ' img'.

dgd_danhmuc.Items[i].Cells[7].Text=="1" -> is a status column in database, it's be hidden column 
in the datagrid. I add a template column like this into the datagrid.

Open in new window

<asp:TemplateColumn HeaderText="T.T">
                            <HeaderStyle Font-Bold="True" ForeColor="#1E63A8" Font-Names="Arial" Wrap="False"
                                Width="3%"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                            <ItemTemplate>
                                <asp:Image ID="Image1" runat="server" Height="16px" Width="16px"></asp:Image>
                            </ItemTemplate>
</asp:TemplateColumn>

Open in new window


After binding data to datagrid, you call the function.
 

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Save json data from URL using SSIS 1 77
How to identify a paragraph in the body of text? 2 82
How to force output to ascii 2 65
parse string in c# 5 41
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question