0

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

2
Contributors
3
Replies
6
Views
7 Years
Discussion Span
Last Post by finito
1

You don't need to do Execute reader just use scaler.

file = (byte[])cmd.ExecuteScaler();

As for Null there are two ways I handle it.

1st Method
Try
Catch

2nd Method

"SELECT CASE WHEN Image IS NULL THEN DefaultImage ELSE Image END WHERE CarID = @CarID"

You can define DefaultImage as (byte[])new Image(URL)

Like

"SELECT CASE WHEN Image IS NULL THEN '" + (byte[])new Image(URL) + "' ELSE Image END WHERE CarID = @CarID"

I haven't tried it for Images but it should work.

Edited by finito: n/a

Votes + Comments
amazing responce
0

Try catch Method is not recommended.

It looks too slacky, you should look for a way to remove nulls from your results so the code is more elegant.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.