Click here to Skip to main content
13,092,310 members (58,010 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am trying to access a url stored in a database to display an image using asp image control based on user selection. When user selects, I would like image displayed.

The column name where the url is stored is "ImgLnk"

So I am trying this code:

<asp:Image ID="image1" runat="server" ImageUrl="ImgLnk" Height="100px" Width="100px" />

And my code behind:
string strConnectionString = ConfigurationManager.ConnectionStrings[""].ConnectionString;
        string strSelect = "SELECT ImgLnk FROM Business WHERE ImgLnk = @ImgLnk";
        SqlConnection sqlCon = new SqlConnection();
        sqlCon.ConnectionString = strConnectionString;
        SqlCommand cmdCustomerDetails = new SqlCommand();
        cmdCustomerDetails.Connection = sqlCon;
        cmdCustomerDetails.CommandType = System.Data.CommandType.Text;
        cmdCustomerDetails.CommandText = strSelect;
        cmdCustomerDetails.Parameters.AddWithValue("@ImgLnk", strImgLnk);
        //Create DataReader to read the record
        SqlDataReader dReader = cmdCustomerDetails.ExecuteReader();
        GridView2.DataSource = dReader;
        while (dReader.Read())
            image1.ImageUrl = HttpUtility.UrlEncode(dReader["ImgLnk"].ToString());

I do not know what else to try. Thanks for help code masters.
Posted 10-Aug-12 19:17pm
Wes Aday 11-Aug-12 1:28am
And the actual problem that you have is....?
Member 8759797 11-Aug-12 11:04am
So sorry! I see your dilemma...

My problem is that in the ImageUrl field I have "ImgLnk" which is a url field in my database. I can't show the picture because I can't convey to ImageUrl to use the file path "ImgLnk" in database to retrieve the picture. Currently ImageUrl is trying to find the path "ImgLnk" as thought ImgLnk were a Url.

I hope this explanation helps you guide me to the correction I need to make as this is the last part of the project and I am stuck! Thanks.
Mohamed Mitwalli 11-Aug-12 2:37am
Hi ,
Make sure of your select statement
string strSelect = "SELECT ImgLnk FROM Business WHERE ImgLnk = @ImgLnk";
in your where condiation you passing Imglnk to get imglnk
Member 8759797 11-Aug-12 11:09am
Yes thank you for your suggestion. I do feel this is a problem. In my ImgUrl I have "ImgLnk", but I don't know how to tell ImageUrl to use the file path in the database column ImgLnk and not to use ImgLnk as the file path.

This process works fine in Gridview when I use DataImageUrlField="ImgLnk". I am hoping to not use Gridview but to show the image directly. Thanks so much for any help you can offer!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Member 8759797 11-Aug-12 11:15am
Thank you for your suggestion. It is helpful and I am sure I can use it. This problem I am having relates to passing a file path stored in a database to the asp:Image option thru the parameter ImageUrl. So the image is not on the database directly but rather the link to the picture. Can you help in this area? I hope so. Thanks for you time!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 11 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100