ashx hnadler error while paasing request and response [Resolved]

Posted by Aaryan under ASP.NET on 3/13/2012 | Points: 10 | Views : 2808 | Status : [Member] | Replies : 22
public void ProcessRequest(HttpContext context)
{
SqlConnection cn = new SqlConnection("Data Source=PRAVIN-PC;Initial Catalog=sales;Integrated Security=True");
cn.Open();
string id1 =context.Request.QueryString["id"].ToString ();
SqlCommand cmd = new SqlCommand("SELECT [image] FROM [image] where id =" + id1, cn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((byte[])dr[image]);
dr.Close();
cn.Close();
}



This is my coding.. am getting error in highlighted line and throws exception error as

"Null reference exception was unhandled by user code.
Object reference not set to an instance of an object. "

pls provide a solution...




Responses

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi aaryan try this

<asp:Image ID="id" runat="server" ImageUrl='<%# "Handler2.ashx?id=" + Eval("id")%>' />

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Windows.Forms;

public class Handler2 : IHttpHandler {

public void ProcessRequest(HttpContext context)

{

SqlConnection cn = new SqlConnection("Data Source=PRAVIN-PC;Initial Catalog=sales;Integrated Security=True");

cn.Open();
if (context.Request.QueryString["id"] != null)
{
string id1 = context.Request.QueryString["id"].ToString();



SqlCommand cmd = new SqlCommand("SELECT [image] FROM [image] where id =" + id1, cn);


SqlDataReader dr = cmd.ExecuteReader();

dr.Read();


if (dr["image"] != null || dr["image"] != DBNull.Value || !string.IsNullOrEmpty(dr["image"].ToString().Trim()))
{

context.Response.BinaryWrite((byte[])dr["image"]);

}


dr.Close();
}
cn.Close();

}
public bool IsReusable {
get {
return false;
}
}

}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Aaryan,
I've doubt in your query.
you might get null result in "image field.
kindly check it

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks for ur reply

yes.. am getting id value and the image value null... how to overcome it...

also canu u tell how to give dataimageurlformatstrieng field server map path to load file in grid

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Here is the solution:

public void ProcessRequest(HttpContext context)
{
SqlConnection cn = new SqlConnection("Data Source=PRAVIN-PC;Initial Catalog=sales;Integrated Security=True");
cn.Open();
string id1 =context.Request.QueryString["id"].ToString ();
SqlCommand cmd = new SqlCommand("SELECT [image] FROM [image] where id =" + id1, cn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if(dr[image] != null || dr[image] != DBNull.Value)
{
context.Response.BinaryWrite((byte[])dr[image]);
}
dr.Close();
cn.Close();
}


and now plz specify your second question in detail.

mark this as answer, if it helps you............

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
context.Response.BinaryWrite((byte[])dr["image"]);
in the above line am getting error as below
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

Note: image s my field name so i added " " to image does it makes any error..

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Aarya, heve you put condition i mentioned in my previous post???

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
s... can u say the solution


Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Here is the solution:

public void ProcessRequest(HttpContext context)
{
SqlConnection cn = new SqlConnection("Data Source=PRAVIN-PC;Initial Catalog=sales;Integrated Security=True");
cn.Open();
string id1 =context.Request.QueryString["id"].ToString ();
SqlCommand cmd = new SqlCommand("SELECT [image] FROM [image] where id =" + id1, cn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if(dr[image] != null || dr[image] != DBNull.Value)
{
context.Response.BinaryWrite((byte[])dr[image]);
}
dr.Close();
cn.Close();
}


mark this as answer, if it helps you............

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
context.Response.BinaryWrite((byte[])dr["image"]);
in the above line am getting error as below
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

Note: image s my field name so i added " " to image does it makes any error

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Here is the solution:

public void ProcessRequest(HttpContext context)
{
SqlConnection cn = new SqlConnection("Data Source=PRAVIN-PC;Initial Catalog=sales;Integrated Security=True");
cn.Open();
string id1 =context.Request.QueryString["id"].ToString ();
SqlCommand cmd = new SqlCommand("SELECT [image] FROM [image] where id =" + id1, cn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if(dr[image] != null || dr[image] != DBNull.Value || !string.IsNullOrEmpty(dr[image].ToString().Trim()))
{
context.Response.BinaryWrite((byte[])dr[image]);
}
dr.Close();
cn.Close();
}


mark this as answer, if it helps you............

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
string id1 =context.Request.QueryString["id"].ToString ();

in this line am getting as

Object reference not set to an instance of an object.

the value of id1 is showing as null when i checked using breakpoint.. how can i solve this

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
check URL from where you redirected to this handler??

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
<asp:Image ID="id" runat="server" ImageUrl='<%# "Handler2.ashx?id=" + Eval("id")%>' />

This s my url...
I dnt knw what is to be changed

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
can u pls reply me

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
I'm not getting where problem could be?

as such i see the code line is correct...
will you attach your code??

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
this my gird code;
<asp:Image ID="id" runat="server" ImageUrl='<%# "Handler2.ashx?id=" + Eval("id")%>' />

this my handler code:
<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Windows.Forms;

public class Handler2 : IHttpHandler {

public void ProcessRequest(HttpContext context)

{

SqlConnection cn = new SqlConnection("Data Source=PRAVIN-PC;Initial Catalog=sales;Integrated Security=True");

cn.Open();
if (context.Request.QueryString["id"].ToString() != null)
{
string id1 = context.Request.QueryString["id"].ToString();



SqlCommand cmd = new SqlCommand("SELECT [image] FROM [image] where id =" + id1, cn);


SqlDataReader dr = cmd.ExecuteReader();

dr.Read();


if (dr["image"] != null || dr["image"] != DBNull.Value || !string.IsNullOrEmpty(dr["image"].ToString().Trim()))
{

context.Response.BinaryWrite((byte[])dr["image"]);

}


dr.Close();
}
cn.Close();

}
public bool IsReusable {
get {
return false;
}
}

}





Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
can u help in setting dataimageurlformatstring using server mappath..


Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
pls rep me


Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: sourabhghosh49-13964 on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
your aspx code must be this...

<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("id") %>'/>
</ItemTemplate>
</asp:TemplateField>


and handler code must be---->>

public class ImageHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
string id1 = context.Request.QueryString["id"];
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand("select Image from Image where id="+id1, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End();

}

public bool IsReusable {
get {
return false;
}
}



if your problem solved then mark this as answer....

sourabh

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
thank u so much... bt am nt getting image... so far there is x mark. now am nt having that bt image is nt displayed why?

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Ok then post this Question in new forum

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Ok,
do one thing....
Creare one folder called Image in you application root directory.
Create all images that are in your Database, in this folder using hamdler and name them <your-id>.jpg or as per your choice
and then write below code:

<asp:Image ID="id" runat="server" ImageUrl='<%# "Image\" + Eval("id") + ".jpg" %>' />



Mark this as answer, if it helps you................

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response