charan sekhar

charan sekhar

  • NA
  • 108
  • 0

displaying image from database giving error

May 16 2010 9:12 AM
 string branchid = "";
protected void Page_Load(object sender, EventArgs e)
{
branchid= Session["branchid"].ToString();

Response.ContentType = "image/jpeg";
Stream strm = ShowImage(branchid);

byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
Image1.imageurl=byteSeq;
}


public Stream ShowImage(string branchid)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Safa"].ConnectionString);
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 null;
}
finally {
con.Close();
}
}

 
hi iam using asp.net with c#

iam retreving image from database but i want to display in

image1.imageurl
but it is not displaying image
can you correct my code

 

Answers (1)