views:

203

answers:

2

Hi all, Previously i had problem with inserting image into sql database. Now i have solved this problem and able to insert image in sqldatabase. Now I am facing problem with retrieving the image from database table. Here is my retrieving code:

showimage.ashx:
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Web;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public class ShowImage : IHttpHandler 
{

 public void ProcessRequest (HttpContext context)
 {
  int empno;
  if (context.Request.QueryString["empid"] != null)
   empno = Convert.ToInt32(context.Request.QueryString["id"]);
  else
   throw new ArgumentException("No parameter specified");
  context.Response.ContentType = "image/jpeg";
  //context.Response.Write("Hello World");
  Stream strm = ShowEmpImage(empno);
  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);        
  }        
 }
 public Stream ShowEmpImage(int empno)
 {
  string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
  SqlConnection connection = new SqlConnection(conn);
  string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
  SqlCommand cmd = new SqlCommand(sql, connection);
  cmd.CommandType = CommandType.Text;
  cmd.Parameters.AddWithValue("@ID", empno);
  connection.Open();
  object img = cmd.ExecuteScalar();
  try
  {
   return new MemoryStream((byte[])img);

  }
  catch
  {
   return null;

  }
  finally
  {
   connection.Close();
  }  
 } 
 public bool IsReusable 
 {
  get
  {
   return false;
  }
 }

}

In this line:

**context.Response.ContentType = "image/jpeg";**

getting exception "No parameter specified"

Pls help me how can i retrieve image from database table. Here is my GUI:

<asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
  <asp:TextBox ID="txtEName" runat="server"></asp:TextBox>
  <br />
  <asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
  <asp:FileUpload ID="imgUpload" runat="server" />
  <br />
  <br />
  <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
   onclick="btnSubmit_Click" />   
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp      
  <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
 <br />
 <hr />  
   <asp:Image ID="Image1" style="width:200px" Runat="server"/>
A: 

The exception you are seeing is actually the one thrown on the line above:

throw new ArgumentException("No parameter specified");

I.e. caused because your request does not have the empid query string:

somepage.aspx?empid=42

Other than that I cant see anything else obviously wrong in your code.

Kragen
A: 

The error message you are getting, looks like is due to empid being missing as Kragen points out.

I think that you could simplify your code a great deal:

  • You have an object that you cast to a byte array
  • You then use the byte array to create a stream
  • You then convert the stream to a byte array.

Why not just return the byte array?

This link may also help you: http://www.worldofasp.net/tut/images/Displaying%5Fimages%5Fin%5FASPNET%5Fusing%5FHttpHandlers%5F92.aspx

Shiraz Bhaiji