Click here to Skip to main content
15,905,073 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon every one,

Sir I m fresher . I m trying to insert image in data base . so I copied that code but My image not show

This is Databse:-
-------------------------
colum Name   datatype
ImageId      Int(set identity property=true)

ImageName     Varchar(50)

Image         image

---------------------------
.cs Code:
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
   if (imageUpload.HasFile)
   {
      //getting length of uploaded file
      int length = imageUpload.PostedFile.ContentLength;
      //create a byte array to store the binary image data
      byte[] imgbyte = new byte[length];
      //store the currently selected file in memeory
      HttpPostedFile img = imageUpload.PostedFile;
      //set the binary data
      img.InputStream.Read(imgbyte, 0, length);
      string imagename = txtImageName.Text;
      SqlConnection con = new SqlConnection(strcon);
      SqlCommand cmd = new SqlCommand("insert into Table_1(u_id,user_name,email,image,image_name) values ('3','yawar','yawr@gmail.com','"+img +"','"+imagename+"')", con);
      cmd.Connection.Open();
      cmd.ExecuteNonQuery();
      cmd.Connection.Close();
   }
}

--------------------------------
grid view:-
XML
</asp:TemplateField>

                 <asp:TemplateField HeaderText="Photo">

                 <ItemTemplate>
                 <asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("u_id")%>' Height="200px" Width="200px"/>
                 </ItemTemplate>

                </asp:TemplateField>

---------------------
<%@ WebHandler Language="C#" Class="ImageHandler" %>

C#
using System;
using System.Web;
using System.Data.SqlClient;
using System.IO;
public class ImageHandler : IHttpHandler
{
   string strcon = "Server=UNIAYUR-PC\\YAWAR; User Id=sa; Password=uniayur123; Database=Gridview_data";
   public void ProcessRequest (HttpContext context)
   {
      string imageid = context.Request.QueryString["ImID"];
      SqlConnection con = new SqlConnection(strcon);
      con.Open();
      SqlCommand cmd = new SqlCommand(" Select image from Table_1 where u_id='" + imageid + "'", con);
      SqlDataReader dr = cmd.ExecuteReader();
      dr.Read();
      context.Response.BinaryWrite((Byte[])dr[0]);
      context.Response.End();
      con.Close();
   }
 
   public bool IsReusable
   {
      get
      {
         return false;
      }
   }
}
Posted
Updated 20-May-14 23:06pm
v3
Comments
KatsuneShinsengumi 21-May-14 4:10am    
You shouldn't insert image in database, images should be stored in separate folder and only insert the url of the images to the database. So when you call the image you'll just get the url from the database.
Karen Mitchelle 21-May-14 4:12am    
Agree..

1 solution

use parameter to insert
C#
cmd = new SqlCommand("insert into Table_1([image]) VALUES (@img)", con);
cmd.Parameters.Add("@img", SqlDbType.Image).Value = imgbyte;
 
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