Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi.
 
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("فایل شما هیچ محتوایی ندارد!");
                    return;
                }
                //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);
 
                try
                {
                    Command.Connection.Open();
                    Command.ExecuteNonQuery();
                    Command.Connection.Close();
                }
                catch (Exception ex)
                {
                    EventLogger.Log(ex.Message, LogType.Error);
                }
                finally
                {
                    DataProvider.DisposeCommand(Command);
                }
 
            }
            //Read the inserted Image Array from database and show the Image inside the box.
            SqlCommand ShowCommand = DataProvider.GenerateCommand("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
            
            try
            {
                ShowCommand.Connection.Open();
                SqlDataReader DataReader = DataProvider.ExecuteDataReader("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
                DataReader.Read();
                PersonelImage.ImageUrl = DataReader["ImageContent"].ToString();
                DataReader.Close();
                ShowCommand.Connection.Close();
            }
            catch (Exception ex)
            {
                EventLogger.Log(ex.Message, LogType.Error);
            }
            finally
            {
                DataProvider.DisposeCommand(ShowCommand);
            }
        }
so what is the problem of my code?

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

Solution 1

hi,
 
you can use asp.net 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 .
 
<html>
<head runat="server">
    <script language="javascript">
 
        function Change(obj) {
            __doPostBack("<%= btnUpload.ClientID %>", "");
        }
 
    </script>
</head>
<body>
    <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">
    </asp:ScriptManager>
&nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
              <img id="imgOrginal" runat="server" style="height: 200px; width: 200px;" />
              </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

protected void btnUpload_Click(object sender, EventArgs e)
{
      imgFileUploader.PostedFile.SaveAs(Server.MapPath("~/image/" + imgFileUploader.FileName));
          imgOrginal.Src = "~/image/" + imgFileUploader.FileName;
}
  Permalink  
v3
Rate this: bad
good
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.
 
Thanks...

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 7 Mar 2013
Copyright © CodeProject, 1999-2014
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