Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to display the picture of Employee as soon as he has Uploaded and also he has the provision of Updating or deleting his Picture.The Images are to be Stored in Database.

Efforts Made:(Upload page)
C#
protected void butSubmit_Click(object sender, EventArgs e)
   {
       //try
       //{
           Byte[] imgByte = null;
           if(FileUpload1.HasFile && FileUpload1.PostedFile !=null)
           {
                HttpPostedFile File = FileUpload1.PostedFile;
               imgByte = new Byte[File.ContentLength];
               File.InputStream.Read(imgByte, 0, File.ContentLength);
           }
        string cs = ConfigurationManager.ConnectionStrings["master"].ConnectionString;
          using( SqlConnection connection = new SqlConnection(cs))
          {
              connection.Open();
              string sql = "INSERT INTO tblImage(image) VALUES(@theImage) SELECT @@IDENTITY";
              using (SqlCommand cmd = new SqlCommand(sql, connection))
              {
                  cmd.Parameters.AddWithValue("@theImage", imgByte);
                  int id = Convert.ToInt32(cmd.ExecuteScalar());
                  lblStatus.Text = String.Format("Image is Uploaded successfully!! and Image ID is {0}", id);
                  cmd.Dispose();
              }
              connection.Close();
              connection.Dispose();
          }


Display Page:
XML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
     <script type="text/javascript">
        $(document).ready(function () {
            $("#Button1").click(function (e) {
                $('.display').text('');   //clean div display
                var ID = $("#txtID").val();
                if (ID.length > 0) {
                    var newImage = $('<img />');
                    newImage.attr('src', 'ReadImage.ashx?id=' + ID); // call handler with id
                    $('.display').append(newImage);
                }
                e.preventDefault();
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     Display Image<br />
    <br />
    Id
    <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" Text="Display" />
    <br />
    <br />
    <div class="display"></div>
    <br />
    <br />
    </div>
    </form>


Not Satisfied with Coding.
Posted

1 solution

I think it is better you try with AJAX toolkit if you are using Dot Net 3.5 onwards. You can see my article from the following link and then add templatefield to perform Edit/Update/Delete o/ps. Happy coding. I Need your feedback after trying.

Practical Guide for Creating HyperLinkField in GridView in ASP.NET[^]
 
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