Click here to Skip to main content
11,709,581 members (49,903 online)
Rate this: bad
Please Sign up or sign in to vote.

I have written the following code to insert the image into the database and then retrive it and show it instantly in the page.

for uploading and inserting step , it works fine , but i can not retrive & show it:
protected void SendImageButton_Click(object sender, EventArgs e)
            if (FileUpload.PostedFile != null && FileUpload.PostedFile.FileName != null)//Specify whether user has posted the file or not.
                HttpPostedFile Picture = FileUpload.PostedFile;
                if (Picture.ContentLength==0) //Check the content of the file
                    Response.Write("فایل شما هیچ محتوایی ندارد!");
                //check the path of the file that has to be valid form.
                //if (Path.GetExtension(Picture.FileName).ToLower()!=".jpg" || Path.GetExtension(Picture.FileName).ToLower()!=".jpeg" || Path.GetExtension(Picture.FileName).ToLower()!=".jpe" ||Path.GetExtension(Picture.FileName).ToLower()!=".jfif")
                //    Response.Write("پسوند این فایل نامعتبر است!");
                //    return;
                //convert the picture into an array form to enable it for inserting into the database, and then read the file.
                byte[] MyImage = new Byte[Picture.ContentLength];
                Picture.InputStream.Read(MyImage, 0, Picture.ContentLength);
                //Insert into the UplaodPicture table by using StoredProcedure.
                SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Insert_UploadPicture_SP]", CommandType.StoredProcedure);
                Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
                Command.Parameters.AddWithValue("@ImageContent", MyImage);
                Command.Parameters.AddWithValue("@ImageSize", Picture.ContentLength);
                Command.Parameters.AddWithValue("@ImageType", Picture.ContentType);
                catch (Exception ex)
                    EventLogger.Log(ex.Message, LogType.Error);
            //Read the inserted Image Array from database and show the Image inside the box.
            SqlCommand ShowCommand = DataProvider.GenerateCommand("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
                SqlDataReader DataReader = DataProvider.ExecuteDataReader("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
                PersonelImage.ImageUrl = DataReader["ImageContent"].ToString();
            catch (Exception ex)
                EventLogger.Log(ex.Message, LogType.Error);
so what is the problem of my code?

i will be thankfull for your help.
Posted 6-Mar-13 19:06pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


you can use image control

PersonelImage.Src = "Your Image Path"; 

If you want to show the photo immedaite to insert into database , then use asynchronous method to show the image , using the ajax toolscript and update panel .

<head runat="server">
    <script language="javascript">
        function Change(obj) {
            __doPostBack("<%= btnUpload.ClientID %>", "");
    <form id="form1" runat="server">
    Upload Image:
        <asp:FileUpload ID="imgFileUploader" runat="server"   />
            <asp:Button ID="btnUpload" runat="server"
        Text="Upload" onclick="btnUpload_Click" onchange="Change(this);" />
    <br />
       <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
&nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
              <img id="imgOrginal" runat="server" style="height: 200px; width: 200px;" />

protected void btnUpload_Click(object sender, EventArgs e)
      imgFileUploader.PostedFile.SaveAs(Server.MapPath("~/image/" + imgFileUploader.FileName));
          imgOrginal.Src = "~/image/" + imgFileUploader.FileName;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can't do like this.
PersonelImage.ImageUrl = DataReader["ImageContent"].ToString();

You can use one handler for this task and assign that to the ImageURL.
// Display the image from the database
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
Follow Save and Retrieve Images from the Database using ASP.NET 2.0 and ASP.NET 3.5[^], which will guide you step by step how to do this task.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 525
1 OriginalGriff 250
2 Peter Leow 219
3 Maciej Los 185
4 Mika Wendelius 160
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,037
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 7 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100