Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    <center>
    <table>
    <tr>
    <td>
    Image Name
    </td>
    <td>
        <asp:TextBox ID="txtImageName" runat="server">
    </td>
    </tr>
    <tr>
    <td>
    Upload IMage
    </td>
    <td>
        <asp:FileUpload ID="fileuploadimage" runat="server" />
    </td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="btnUpload" runat="server" Text="Upload" 
            onclick="btnUpload_Click"  />
    </td>
    </tr>
    </table>
    
    </center>
    
    </div>
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <columns>
                <asp:BoundField DataField="imagename" HeaderText="Image Name" />
                
                <asp:TemplateField HeaderText="Image">
                <itemtemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl ='<%#"ImageHandler.ashx?ImID="+Eval("ImageID") %>' height="150px" Width="150px"/>
                </itemtemplate>
                
            </columns>
        
    
    
    </div>
    </form>
</body>
</html>

CS CODE:
C#
string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridData();
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fileuploadimage.HasFile)
        {
            int length = fileuploadimage.PostedFile.ContentLength;
            byte[] imgbyte = new byte[length];
            HttpPostedFile img = fileuploadimage.PostedFile;
            img.InputStream.Read(imgbyte, 0, length);
            string imagename = txtImageName.Text;

            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Image(ImageName,Image)values(@imagename,@imagedata)", con);
            cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
            cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
            int count = cmd.ExecuteNonQuery();
            con.Close();
            if (count == 1)
            {
                BindGridData();
                txtImageName.Text = string.Empty;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Added Successfully')", true);

            }
        }
    }
    public void BindGridData()
    {
        SqlConnection connection = new SqlConnection(strcon);
        SqlCommand command = new SqlCommand("select imagename,imageID from[Image]",connection);
        SqlDataAdapter daimage = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        daimage.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        GridView1.Attributes.Add("bordercolor", "black");
    }
}

ASHX CODE:
HTML
%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class ImageHandler : IHttpHandler 
{
    string strcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
    public void ProcessRequest (HttpContext context)
    {
        string imageid = context.Request.QueryString["ImID"];
        SqlConnection connection = new SqlConnection(strcon);
        connection.Open();
        SqlCommand command = new SqlCommand("select Image from Image where ImageID=" + imageid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        connection.Close();
        context.Response.End();
     }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
Posted
Updated 25-Jun-13 2:16am
v3
Comments
Pallavi Waikar 25-Jun-13 8:53am    
Your <asp:GridView is missing closing tag check it
nelson edberc 25-Jun-13 8:57am    
Still it's not working
Pallavi Waikar 25-Jun-13 9:03am    
Your referring this article compare with it http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html
nelson edberc 25-Jun-13 9:27am    
yes,i want to include this in my form.but it doesn't work.......
Debug and find out what is happening inside the Handler.

1 solution

Problem, which I can see

1. In the below line, you are giving the ImageUrl as the "ImageID" from the BindingSource.
XML
<asp:Image ID="Image1" runat="server" ImageUrl ='<%#"ImageHandler.ashx?ImID="+Eval("ImageID") %>' height="150px" Width="150px"/>

But while binding you are reading "imageID".
C#
SqlCommand command = new SqlCommand("select imagename, imageID from[Image]",connection);

The names are different. So, I suspect the IDs are not getting added to the ImageUrl correctly.

But you need to debug and see whether the ID is getting added to the rows and the same ID is going to the Handler or not.

2. There is a space needed after "from" and before "[Image]" in the below query.
C#
SqlCommand command = new SqlCommand("select imagename, imageID from [Image]",connection);
 
Share this answer
 
v3

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