Solved

Image from sql database to gridview

Posted on 2011-02-21
6
421 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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