You must Sign In to post a response.
  • Category: .NET

    How to retrieve image from datbase and show in image control

    i am inserting data and images into datbase it works fine whileretrieving all textboxes values are retrieving but my image control image not retrieving
    so below is my code wherei am wrong please helpme

    this code for retrieving

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
    string id = TextBox1.Text;


    Image2.Visible = id != "0";
    if (id != "0")
    {
    byte[] bytes = (byte[])GetData("select pc from [ektha employeea] where [Employee_ Id] =" + id).Rows[0]["pc"];
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
    Image2.ImageUrl = "data:image/png;base64," + base64String;

    }
    }
    this code for inserting

    protected void Button3_Click(object sender, EventArgs e)
    {
    FileUpload img = (FileUpload)FileUpload1;
    Byte[] imgByte = null;

    if (img.HasFile && img.PostedFile != null)
    {
    //To create a PostedFile
    HttpPostedFile File = FileUpload1.PostedFile;
    //Create byte Array with file len
    imgByte = new Byte[File.ContentLength];
    //force the control to load data in array
    File.InputStream.Read(imgByte, 0, File.ContentLength);
    }
    else
    {
    HttpPostedFile Filea = FileUpload1.PostedFile;
    imgByte = new Byte[Filea.ContentLength];
    Filea.InputStream.Read(imgByte, 0, Filea.ContentLength);
    }

    FileUpload imga = (FileUpload)FileUpload2;
    Byte[] imgBytebb = null;


    if (imga.HasFile && imga.PostedFile != null)
    {
    //To create a PostedFile
    HttpPostedFile File = FileUpload1.PostedFile;
    //Create byte Array with file len
    imgBytebb = new Byte[File.ContentLength];
    //force the control to load data in array
    File.InputStream.Read(imgBytebb, 0, File.ContentLength);
    }
    else
    {
    HttpPostedFile Filea = FileUpload1.PostedFile;
    imgBytebb = new Byte[Filea.ContentLength];
    Filea.InputStream.Read(imgBytebb, 0, Filea.ContentLength);
    }
    string Degreeconvocationcertificate = CheckBox3.Checked ? "Yes" : "N0";
    string Degreeprovisionalcertificate = CheckBox4.Checked ? "Yes" : "N0";
    string Degreeconsolidatememorandumofmarks = CheckBox5.Checked ? "Yes" : "N0";
    string IntermediateMemo = CheckBox6.Checked ? "Yes" : "N0";
    string sscmemo = CheckBox7.Checked ? "Yes" : "N0";
    string pancard = CheckBox8.Checked ? "Yes" : "N0";
    string anyotheridproof = CheckBox9.Checked ? "Yes" : "N0";
    string powerbillofcurrentaddress = CheckBox10.Checked ? "Yes" : "N0";
    string relievingletter = CheckBox11.Checked ? "Yes" : "N0";
    string experienceletter = CheckBox12.Checked ? "Yes" : "N0";
    string salarycertificate = CheckBox13.Checked ? "Yes" : "N0";
    string otherApreciationorincrementletter = CheckBox14.Checked ? "Yes" : "N0";
    string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand("insert into [ektha employeea] values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + CheckBox1.Text + "','" + Degreeconvocationcertificate + "','" + Degreeprovisionalcertificate + "','" + Degreeconsolidatememorandumofmarks + "','" + IntermediateMemo + "','" + sscmemo + "','" + pancard + "','" + anyotheridproof + "','" + powerbillofcurrentaddress + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + TextBox14.Text + "','" + TextBox15.Text + "','" + TextBox16.Text + "','" + TextBox17.Text + "','" + relievingletter + "','" + experienceletter + "','" + salarycertificate + "','" + otherApreciationorincrementletter + "','" + TextBox18.Text + "','" + TextBox19.Text + "','" + TextBox20.Text + "','" + TextBox22.Text + "','" + TextBox23.Text + "','" + img + "','" + imga + "','" + DropDownList1.SelectedValue + "','" + TextBox21.Text + "','" + TextBox24.Text + "')", con);
    //SqlCommand cmd = new SqlCommand("insert into [ektha_employeea]([Employee_ Id],[FirstName],[MiddleName],[lastname],[Father Name],[DOB],[Dob original],[Blood Group],[Bank Account Details],[Do you have all your certificates],[Degree convocation Certificate],[Degree Provsional Certificate],[Degree consolidate memorandum of marks],[Intermediate Memo],[sscmemo,pancard],[Any other photo Id proof],[Address proof power bill of current address],[present address],[permanent address],[Emailid],[pancardnumber],[martial status],[spouse/guardian name] ,[last employer],[experience-no of years],[Relieving letter],[Experience letter],[salary certificate],[appreciation/increment letter],[other],[Last drawn salary in gross per month],[Date of joining at ektha],[references by],[password],[pc],[certificate image],[Educational qualification]) values(@[Employee_ Id],@[FirstName],@[MiddleName],@[lastname],@[Father Name],@[DOB],@[Dob original],@[Blood Group],@[Bank Account Details],@[Do you have all your certificates],@[Degree convocation Certificate],@[Degree Provsional Certificate],@[Degree consolidate memorandum of marks],@[Intermediate Memo],@[sscmemo],@[pancard],@[Any other photo Id proof],@[Address proof power bill of current address],@[present address],@[permanent address],@[Emailid],@[pancardnumber],@[martial status],@[spouse/guardian name] ,@[last employer],@[experience-no of years],@[Relieving letter],@[Experience letter],@[salary certificate],@[appreciation/increment letter],@[other],@[Last drawn salary in gross per month],@[Date of joining at ektha],@[references by],@[password],@[pc],@[certificate image],@[Educational qualification]) ", con);

    con.Open();
    cmd.ExecuteNonQuery();

    con.Close();

    }

    }


    }
  • #764221
    when you retrieve image from database it fetch in the form of ByteArray so you need to then convert that ByteArray to physical image file and then only you can retrieve it.
    Here one thing to keep in mind that the image format (e.g. jpg, png, gif) which you have saved in database has to retrieve in same format from database, means if you save .jpg file in database you should retrieve it as JPG only
    see below snippet
    byte[] objByte = objDS.Tables[0].Rows[1][0];
    //create physical image file
    System.IO.File.WriteAllBytes("D:\\test.jpg", objByte);

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764238

    1. Create ashx handler
    2. Retrieve the image bites in the handler from the database. And return the image content
    3. Assign the url to your image control.

    In the ASPX Page
    Image2.ImageUrl = "~/AvatarImageFetch.ashx

    Handle the following sample code in the ashx handler.

    SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["FMMImages"].ConnectionString);

    myConnection.Open();
    string sql = "select largeimage from images_temp where id=@memberid";
    SqlCommand cmd = new SqlCommand(sql, myConnection);
    int param;
    int.TryParse(context.Request.QueryString["memberid"], out param);
    cmd.Parameters.Add("@memberid", SqlDbType.Int).Value = param;
    //cmd.Parameters.Add("@GuID", SqlDbType.UniqueIdentifier).Value = context.Request.QueryString["UID"].ToString();

    cmd.CommandType = System.Data.CommandType.Text;

    SqlDataReader dReader = cmd.ExecuteReader();
    dReader.Read();
    context.Response.BinaryWrite((byte[])dReader["largeimage"]);
    dReader.Close();
    myConnection.Close();


    By Nathan
    Direction is important than speed


Sign In to post your comments