Solved

Image from sql database to gridview

Posted on 2011-02-21
6
416 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
  • 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now