Hey guys,
i want to check if reader.read has a null value, however im struggling to do this.. any ideas?
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Collections.Specialized;
public class Handler : IHttpHandler {
public string GetConnectionString()
{ //Gets Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
}
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["CarID"];
if (id != null)
{
MemoryStream memoryStream = new MemoryStream();
SqlConnection connection = new SqlConnection(GetConnectionString());
string sql = "SELECT Image FROM Car WHERE CarID = @CarID";
//creates new sql command
SqlCommand cmd = new SqlCommand(sql, connection);
//add paramaters to command
cmd.Parameters.AddWithValue("@CarID", id);
//open connection
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (!reader.Read().Equals(System.DBNull.Value))
{
file = (byte[])reader["Image"];
}
else
{
FileStream fs = File.OpenRead(HttpContext.Current.Server.MapPath("~/NoImage.jpg"));
file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
}
reader.Close();
connection.Close();
memoryStream.Write(file, 0, file.Length);
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
memoryStream.Dispose();
}
}
public bool IsReusable {
get {
return false;
}
}
}
thanks