Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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.NET
<asp:Image ID="image1" runat="server" ImageUrl="ImgLnk" Height="100px" Width="100px" />


And my code behind:
HTML
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);
       
        sqlCon.Open();
 
        //Create DataReader to read the record
        SqlDataReader dReader = cmdCustomerDetails.ExecuteReader();
       

        GridView2.DataSource = dReader;
        GridView2.DataBind();
        while (dReader.Read())
        {

            image1.ImageUrl = HttpUtility.UrlEncode(dReader["ImgLnk"].ToString());
        } 
        sqlCon.Close();


I do not know what else to try. Thanks for help code masters.
Posted
Comments
[no name] 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

 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900