Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Problem with storing and Retriving image from database pls help..
Retrive.aspx
C#
<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");
                //cmd.Dispose();
                con.Close();  
}
insert.aspx
Posted
Updated 31-Jan-12 1:19am
v2
Comments
Sanjay K. Gupta 31-Jan-12 7:18am    
What error you are facing, mention about it.
Jignesh J patel 31-Jan-12 7:22am    
value is stored in database but it will not retrive from database...

1 solution

Is it not being retrieved from the database, or just not displaying? These are two different problems. Try to isolate the problem. First determine if the problem is with the database, then the database code, then the code, etc.

Also, for your insert statement, NEVER accept unvalidated user input and concatenate a SQL statement. EVER. You are asking for security issues. Read about SQL injection attacks.
 
Share this answer
 
Comments
Jignesh J patel 31-Jan-12 8:48am    
IT IS NOT DISPLAYING ON WEBPAGE..
Anuja Pawar Indore 1-Feb-12 4:59am    
Don't use full caps, it's shouting.

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