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>
<asp:TextBox ID="txtEName" runat="server"></asp:TextB开发者_运维知识库ox>
<br />
<asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label>
<asp:FileUpload ID="imgUpload" runat="server" />
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
 
<asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
<br />
<hr />
<asp:Image ID="Image1" style="width:200px" Runat="server"/>
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.
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_images_in_ASPNET_using_HttpHandlers_92.aspx
精彩评论