1

I am trying to load an image through label using this code

private void getData()
    {
        SqlConnection conn = new SqlConnection("Data Source = localhost\\SQLEXPRESS;Initial Catalog = MejOnlineManagementDB00;Integrated Security=True;");
        conn.Open();
        SqlCommand sqlCmd = new SqlCommand(@"SELECT empName,empLname,empMi,empImage
                                            FROM employees
                                            WHERE empName = '"+ ddlAvail.SelectedItem.Value.ToString() +"'", conn);
        SqlDataReader rdr = sqlCmd.ExecuteReader();

        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                lblName.Text = rdr.GetValue(0).ToString(); 
                lblLname.Text = rdr.GetValue(1).ToString();
                lblMi.Text = rdr.GetValue(2).ToString();
                lblImage.Text = "<img runat='server' src='" + rdr.GetValue(3).ToString() + "'></img>";
            }
        }
        conn.Close();
    }

every time I select a dropdownlist value it generates me set of sql data.My only problem is the image.Cause it wont load any image at all.The filepath on my visual studio and image name is correct. This is an example of my image ../Images/Profile/logo.jpg I save the images filepath on my database.

rai nalasa
  • 849
  • 1
  • 12
  • 32

2 Answers2

1

Your code looks fine

Make sure the file path your are retrieving from the database exists and the path isn't returning any unwanted characters.

That is:

rdr.GetValue(3).ToString()  //should return the right path (string)

It should be fine. Hope this helped

jamiedanq
  • 967
  • 7
  • 12
0

It won't ever show the image because all that your code is doing is setting the text on the label.

If you want to display the image try this , or this.

Community
  • 1
  • 1
Barry O'Kane
  • 1,189
  • 7
  • 12