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

Recommended Answers

All 3 Replies

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.

commented: amazing responce +2

i used the try catch method, amazing thank you so much!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.