0

I have to display student name and image on my web page while selecting student id from the drop down list. The image is stored in var binary format on db. How can I retrieve the image and display on image box. The given below code is only shows the student first name and last name. How can I display the image without using http generic handler page? Please help me.

Code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet1TableAdapters.TextBoxTableTableAdapter tx;
        tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
        DataTable dt = new DataTable();
        dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));

        foreach (DataRow row in dt.Rows)
        {
            TextBox1.Text = (row["FirstName"].ToString());
            TextBox2.Text = (row["SecondName"].ToString());
        }
    }

SQL Query:

SELECT FirstName, SecondName, StudentImage FROM TextBoxTable WHERE (Id = @Id)

Aspx Source:

<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" />
</div>

Data Base:

enter image description here

Vipin
  • 261
  • 6
  • 20
  • 44
  • You can't. At least you can't easily. There is a way to embed images in web pages: http://www.websiteoptimization.com/speed/tweak/inline-images/ but it would be infinitely easier to write the handler you didn't want to write. If you were using MVC and WebAPI it would be easier still. – Ian Mercer Oct 17 '14 at 05:02
  • @IanMercer : OK. then how can I add generic http handler for above code. I want to display the image by selecting id from the drop down list. Can you please post sample code for this code. – Vipin Oct 17 '14 at 05:14

1 Answers1

1

Code

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataSet1TableAdapters.TextBoxTableTableAdapter tx;
    tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
    DataTable dt = new DataTable();
    dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));
    foreach (DataRow row in dt.Rows)
    {
        TextBox1.Text = (row["FirstName"].ToString());
        TextBox2.Text = (row["SecondName"].ToString());  
        byte[] barrImg = (byte[])(row["StudentImage"].ToString());
        string base64String = Convert.ToBase64String(barrImg , 0, barrImg.Length);
        Image1.ImageUrl = "data:image/png;base64," + base64String;
    }
}'

I think this code will work for you

  • While using your code getting an error. The error is Cannot convert type 'string' to 'byte[]' – Vipin Oct 17 '14 at 05:37
  • use this link http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array to convert string to byte array – Arindam Nayak Oct 17 '14 at 06:05