Solved

Image from sql database to gridview

Posted on 2011-02-21
6
430 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

Technology Partners: 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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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