Problem with storing and Retriving image from database pls help..
Retrive.aspx
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ImageName], [Image], [STUDENT_ID], [FNAME] FROM [STUDENT] ORDER BY [STUDENT_ID]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ImageName" HeaderText="ImageName"
SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image runat="server" id="image1" ImageUrl='<%# Eval("ID","Handler.ashx?ID={0}") + Eval("ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="STUDENT_ID" HeaderText="STUDENT_ID" ReadOnly="True"
SortExpression="STUDENT_ID" />
<asp:BoundField DataField="FNAME" HeaderText="FNAME" SortExpression="FNAME" />
</Columns>
</asp:GridView>
Handler.asax
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class Handler : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
SqlDataReader dr = null;
SqlConnection con = null;
SqlCommand cmd = null;
try
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
cmd.CommandText = "select IMAGENAME,IMAGE from STUDENT where ID=@ID";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
con.Open();
dr = cmd.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((byte[])dr["Image"]);
dr.Close();
con.Close();
}
catch (Exception ex)
{
}
}
public bool IsReusable {
get {
return false;
}
}
string strImageName = FileUpload1.FileName.ToString();
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName == "")
{
byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadimage = FileUpload1.PostedFile;
uploadimage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
String sr = "Insert into STUDENT(ImageName,Image,STUDENT_ID,FNAME,MNAME,LASTNAME,MOTHERNAME,LOCALADDRESS,DISTRICT,STATE,PINCODE,PERMENANTADDRESS,DISTRICT1,STATE1,PINCODE1,LANDLINENO,MOBILENO,EMAIL_ID,SEX,DATEOFBIRTH) Values(@ImageName,@Image,'"+STUDENT_ID.Text.ToUpper() + "','" + FIRSTNAME.Text.ToUpper() + "','" + M_NAME.Text.ToUpper() + "','" + LNAME.Text.ToUpper() + "','" + MOTHERNAME.Text.ToUpper() + "','" + LADDRESS.Text.ToUpper() + "','" + DISTRICT.Text.ToUpper() + "','" + STATE.Text.ToUpper() + "','" + PINCODE.Text.ToUpper() + "','" + P_ADDRESS.Text.ToUpper() + "','" + DISTRICT1.Text.ToUpper() + "','" + STATE1.Text.ToUpper() + "','" + PINCODE1.Text.ToUpper() + "','" + LANDLINE_NO.Text.ToUpper() + "','" + MOBILE_NO.Text.ToUpper() + "','" + EMAIL_ID.Text.ToUpper() + "','" + RadioButtonList1.SelectedItem.Text.ToUpper() + "','" + BasicDatePicker1.Text.ToUpper() + "')";
cmd = new SqlCommand(sr, con);
SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);
ImageName.Value = strImageName.ToString();
cmd.Parameters.Add(ImageName);
SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imagesize.Length);
UploadedImage.Value = imagesize;
cmd.Parameters.Add(UploadedImage);
con.Open();
int result= cmd.ExecuteNonQuery();
con.Close();
if(result>0)
msgBox2.alert("STUDENT record created Successfully");
con.Close();
}
insert.aspx