简体   繁体   中英

I can't seem to display the image that i saved in ms access database in asp.net

I have table called tbl_studentdetails in which the details of the student are stored when they create a profile.In that form students are supposed to insert a profile picture as well. For that i have created 3 more fields in my table called image_name(short Text) img_size(number), img_data(Ole-Object). The image is saved in the access database but when i try display it on a different page ,it isn't being displayed. There is no error showing and the image data is also fetched from the table properly but it isn't getting displayed on the screen .

Here is code when i save the image in database by converting it into byte array.

 protected void btn_create_Click(object sender, EventArgs e)
    {
        HttpPostedFile postedfile = FileUpload1.PostedFile;
        String filename = Path.GetFileName(postedfile.FileName);
        String fileextension = Path.GetExtension(filename);
        int filesize = postedfile.ContentLength;

        if(fileextension.ToLower()==".jpg" || fileextension.ToLower() == ".png")
        {
            Stream stream = postedfile.InputStream;
            BinaryReader binaryReader = new BinaryReader(stream);
            byte[] bytes = binaryReader.ReadBytes((int)stream.Length);

            con.Open();
            OleDbCommand cmd = new OleDbCommand("insert into studentdetails(s_name,age,phone_no,city,state,email,qualification,field,years_exp,description,image_name,img_size,img_data) values('" + txt_name.Text + "'," + txt_age.Text + "," + txt_phone.Text + ",'" + txt_city_stud.Text + "','" + txt_state_stud.Text + "','" + txt_mail.Text + "','" + ddl_qualifiy.SelectedValue + "','" + txt_field.Text + "'," + txt_years.Text + ",'" + txt_extra.Text + "','" + filename + "'," + filesize+",'"+bytes+"')");
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Profile created');</script>");

            con.Close();
            
           // Session["create_smail"] = txt_mail.Text;
            Response.Redirect("~/profilepage.aspx?email=" + txt_mail.Text);

        }

and here is the code for when i try to retrieve and display it on the profile page of student on page load

 create_email = Request.QueryString["email"];
            OleDbCommand cmd = new OleDbCommand("select img_data from studentdetails where email='" + create_email + "'", con);
            byte[] bytes = (byte[])cmd.ExecuteScalar();
            string strbase64 = Convert.ToBase64String(bytes, 0, bytes.Length);
            image1.ImageUrl = "data:image/jpg;base64," + strbase64;

image1 is the id of the asp:image tag where the picture is to be displayed.

Can Someone please help me since i have to submit a project soon.

object lining and embedding is VERY but VERY different then just binary file representation.

That ole object ALSO includes and contains SIGNIFICANT information about WHAT application to launch, and HOW to imbed it inside of Access.

So if it is Excel or word, or whatever? Access creates a "instance" of that given application inside of access, and thus uses THAT to render the application - kind of VERY little to do with a binary file. so, this is simular to ActiveX in which a HOST application is used to render the content. So, that ole object column has MUCH more then just binary file data. Too bad, the original Access application did not use a raw binary file. But, whoever set this up did not! As noted, you can imbed Excel, or even power-point applications. Access will thus use + create a application object based on that information. It is then the application that then goes to work to render the "whatever" system or application is required. That's quite much what ole does and is (Object Linking and Imbedding).

and worse, Access also creates + stores a raw BMP file to represent the thumb nail. Now, if you know anything about HOW remarkable picture compression is? Well, often the thumb nail bmp turns out to be MUCH larger then the jpg or whatever picture!! (by significant amounts - so you have to skip the bmp preview image to get the raw file - and you also have to skip the ole information on top of that!!!).

So, you need code to ignore and parse out (remove) the ole embedding information, and inside of that "mess", you then have to pull out the raw binary file.

How do do this is outlined here:

Convert Access image OLE Object into raw image byte array in C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM