1
Answer

how to display nopic.jpg in a image control when there is no image in a database

charan sekhar

charan sekhar

14y
1.9k
1

hi iam using asp.net with c#
i want to display nopic.jpg from my folder images when there is no image in a database
 
can you correct my code
 
 

 
<%
@ WebHandler Language="C#" Class="left1" %>
using
System;
using
System.IO;
using
System.Data;
using
System.Data.SqlClient;
 
 

using
System.Web;
public
class left1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string branchid = "";

if(context.Session["branchid"]!=null)
//if (context.Request.QueryString["Shop"] != null)
branchid= context .Session[
"branchid"].ToString();
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType =
"image/jpeg";
Stream strm = ShowImage(branchid);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
 
 
public Stream ShowImage(string branchid)
{
SqlConnection con = new SqlConnection(" Data Source=.; Initial Catalog=SoftView;User ID=sa;Password=ns3;");
string sql = "SELECT imgleft1 FROM tblImages WHERE BranchId = @branchid";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType =
CommandType.Text;
cmd.Parameters.AddWithValue(
"@BranchId", branchid);
con.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return System.IO.Stream("~/images/NoPic.jpg");
}
finally
{
con.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
 
 

 
and in page load
 
protected
void Page_Load(object sender, EventArgs e)
{
string branchid="";
branchid= Session[
"branchid"].ToString();
if (IsPostBack == false)
{
Image1.ImageUrl =
"left1.ashx?BranchId=" + branchid;
Image2.ImageUrl =
"left1old.ashx?BranchId=" + branchid;



}
}

 

 
 
can you correct my code  where iam going wrong
Answers (1)