Click here to Skip to main content
15,881,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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:
C#
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

hi,

you can use asp.net image control

C#
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
<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>


C#
protected void btnUpload_Click(object sender, EventArgs e)
{
      imgFileUploader.PostedFile.SaveAs(Server.MapPath("~/image/" + imgFileUploader.FileName));
          imgOrginal.Src = "~/image/" + imgFileUploader.FileName;
}
 
Share this answer
 
v3
You can't do like this.
C#
PersonelImage.ImageUrl = DataReader["ImageContent"].ToString();


You can use one handler for this task and assign that to the ImageURL.
C#
// 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...
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900