We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,756 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to put check to varifiy if data is available or not

using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
       Int32 empno;
       if (context.Request.QueryString["id"] != null)
            empno = Convert.ToInt32(context.Request.QueryString["id"]);
       else
            throw new ArgumentException("No parameter specified");

       context.Response.ContentType = "image/jpeg";
       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);
       }       
       //context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)
    {

        SqlConnection con = ConnectionManager.GetConnection();
        string sql = "SELECT emp_pic FROM employee WHERE emp_id = @ID";
        SqlCommand cmd = new SqlCommand(sql,con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", empno);
        con.Open();

        object img = cmd.ExecuteScalar();

        // how to verify if data is not available in database

        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


}
3
Contributors
2
Replies
1 Day
Discussion Span
11 Months Ago
Last Updated
3
Views
silent.saqi
Newbie Poster
8 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

what exactly do you mean by not available?

you could try something like

if (img == null || img == DBNull.Value) { // no data returned
Hearth
Posting Whiz
300 posts since Apr 2008
Reputation Points: 123
Solved Threads: 44
Skill Endorsements: 4

If you have any sort of control over the database, maybe putting images in a different table and having a default image if the user has/doesn't have an image uploaded is a good idea. You can link to it with a foreign key to link employees with their images and have a generic image that reads "Image Not Available" that you link no-image employees to. This will solve a lot of the client-side logic that you need to write and will save ever having to store duplicate images (just link them to the same image rather than store seperate images). Just my two cents.

As for your actual question - what Hearth said will work just fine.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0703 seconds using 2.68MB