Click here to Skip to main content
16,021,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi i have some prob , wen i execute i dint get my image displayed in the gridview ,i aint getting any error>>
httphandler (generic handler)>>
C#
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";
        con.Open();
        SqlCommand command = new SqlCommand("SELECT * from image ", con);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        con.Close();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

default page>>
i have an uploader control , the image is inserted into the db successfully , but cant retrieve it>>
sing System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;
//using System.Web.

public partial class _Default : System.Web.UI.Page
{
    
    //con.ConnectionString =SqlConnection con = new SqlConnection();

  public SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";
          


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
             
            con.Open();
            byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile myimg = FileUpload1.PostedFile;
            myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);

            SqlCommand cmd = new SqlCommand("insert into image values(22,'"+ img +"')",con);
         
            cmd.ExecuteNonQuery();

            con.Close();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    { DataSet ds = new DataSet();
    con.Open();
    SqlCommand command = new SqlCommand("SELECT id from image ", con);
  //  SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection);
    SqlDataAdapter daimages = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    daimages.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    GridView1.Attributes.Add("bordercolor", "black");
                       
   }
    
}
Posted
Updated 17-Feb-12 3:13am
v3
Comments
Anuja Pawar Indore 17-Feb-12 9:13am    
Added pre tag

Hi,
First you call that HTTPHandler functionality and you specify which field you need to access like ,
C#
SqlCommand command = new SqlCommand("SELECT [Photo/Image] from tablename", con);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[Photo]);

And From UI page you call this HTTP Handler like
ASP.NET
<asp:image imageurl="<%# "ReadImage.ashx?PID="+ Eval("PersonID") %>" runat="server" xmlns:asp="#unknown" />


ReadImage.ashx? - is name of HTTPHandeler. finally you can get image from database.


Regards,
anbukrish.
 
Share this answer
 
v2
hi
anbukrish thanks for replying , but still i have some prob , the colum name is imagefile , when i type the name its highlighted as error and states imagefile not found, and i dint get wats this ,where should i write it
C#
<asp:image imageurl="<%# "ReadImage.ashx?PID="+ Eval("PersonID") %>" runat="server" xmlns:asp="#unknown" />

 public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";
        
        con.Open();
        SqlCommand command = new SqlCommand("SELECT imagefile from image ", con);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[imagefile]);
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        con.Close();
        context.Response.End();
    }
 
Share this answer
 
v3
i got u now , all the data is displayed in the gridview , but i cant display the image its displayed as a X enclosed in a square. wat should i do now?
 
Share this answer
 
this is my modified code please help me

here i have included the files of my project , please help me
[B]DEFAULT.ASPX.CS[/B]
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;

//using System.Web.

public partial class _Default : System.Web.UI.Page
{

//con.ConnectionString =SqlConnection con = new SqlConnection();

public SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";

//if(!Roles.RoleExists("admin"))
//{}

}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{

con.Open();
byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile myimg = FileUpload1.PostedFile;
myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);

SqlCommand cmd = new SqlCommand("insert into image values(22,'"+ img +"')",con);

cmd.ExecuteNonQuery();

con.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{ DataSet ds = new DataSet();
con.Open();
SqlCommand command = new SqlCommand("SELECT id from image ", con);
// SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Attributes.Add("bordercolor", "black");



Image img = new Image();
// ImageMap1.DataSource = ds;



}

}
[B]WEB.CONFIG[/B]



<configuration>
<configsections>
<sectiongroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectiongroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

<sectiongroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">








<appsettings>
<connectionstrings>
<add name="imagekConnectionString" connectionstring="Data Source=home-pc\sqlexpress;Initial Catalog=imagek;Integrated Security=True">
providerName="System.Data.SqlClient" />



<system.web>

<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">


<authentication mode="Windows">

<pages>
<controls>
<add tagprefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add tagprefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">


<httphandlers>
<remove verb="*" path="*.asmx">
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false">

<httpmodules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">


<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warninglevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<provideroption name="CompilerVersion" value="v3.5">
<provideroption name="WarnAsError" value="false">

<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warninglevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<provideroption name="CompilerVersion" value="v3.5">
<provideroption name="OptionInfer" value="true">
<provideroption name="WarnAsError" value="false">




<system.webserver>
<validation validateintegratedmodeconfiguration="false">
<modules>
<remove name="ScriptModule">
<add name="ScriptModule" precondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

<handlers>
<remove name="WebServiceHandlerFactory-Integrated">
<remove name="ScriptHandlerFactory">
<remove name="ScriptHandlerFactoryAppServices">
<remove name="ScriptResource">
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" precondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" precondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add name="ScriptResource" precondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">


<runtime>
<assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentassembly>
<assemblyidentity name="System.Web.Extensions" publickeytoken="31bf3856ad364e35">
<bindingredirect oldversion="1.0.0.0-1.1.0.0" newversion="3.5.0.0">

<dependentassembly>
<assemblyidentity name="System.Web.Extensions.Design" publickeytoken="31bf3856ad364e35">
<bindingredirect oldversion="1.0.0.0-1.1.0.0" newversion="3.5.0.0">




[B] GENERIC HANDLER[/B]
]]>

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

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

string imageid = context.Request.QueryString["ImID"];
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";

con.Open();
SqlCommand command = new SqlCommand("SELECT imagefile from image ", con);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
con.Close();
context.Response.End();
}

public bool IsReusable {
get {
return true ;
}
}

}
[B]DEFAULT ASPX[/B]
]]>
 
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