Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Image from sql database to gridview

Posted on 2011-02-21
6
Medium Priority
?
435 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

721 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