Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have database with EmpNo(Int) and EmpImage(Image) columns.

I am using HttpHandler to display the Images.

I am storing Images both in database and folder.

Now I want to change the names of Images in folder as names of EmpNo whose I didnt change while uploading.

So need to fetch the Images names from database to compare them with the Image names in the folder and rename them.

How can i fetch or extract the image names from the binary data that i get from database using generic handler.

I have attached the code In handler for reference.

C#
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class Lab_14___ImageFetchingHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        SqlConnection vConn = new SqlConnection("server=localhost; database=Asp.netDemoWebsiteDatabase; Integrated Security = SSPI;");
        vConn.Open();
        String vQuery = "Select EmpImage from EmpImages where Empno=@id";
        SqlCommand vComm = new SqlCommand(vQuery, vConn);
        
        //Receive the Id from some Form
        String vId = context.Request.QueryString["id"];
        vComm.Parameters.AddWithValue("@id", vId);
        SqlDataReader vDr = vComm.ExecuteReader();
        while (vDr.Read())
        {
            context.Response.ContentType = "image/jpg";
            context.Response.BinaryWrite((byte[])vDr["EmpImage"]);
            
            [ Here I need the Images names to store in List or array.How?? ]
        }
        vConn.Close();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}


What I have tried:

I am thinking some how I can get the file information from SqlDataReader but no idea how to implement it.
Is there any properties or fields in SqlDataReader to get the details of the file??
Posted
Updated 5-Jun-16 13:28pm
v2

1 solution

If you're not storing the image name in the database you cant magically expect SqlDataReader to invent a name for you. I can think of two options, but, the first of those would depend on the image name being part of the metadata for the image - as in EXIF data - so, ignoring that ...

Option 2 : How about an easier approach - given you have an Employee ID and an associated image from your database, the only thing I can think of is you 'name' the image using the Employee ID (possibly padded with '0's to make the same length) and append ".jpg" (or whatever image extension you need) to that

So, instead of

Quote:
[ Here I need the Images names to store in List or array.How?? ]


you use something like

C#
// derive name from Employee ID
string imageName = getImageNameFromEmployeeID(vID, ".jpg");


where getImageNameFromEmployeeID looks like

C#
string getImageNameFromEmployeeID(int vID, string defaultExtension)
{
   // Pad Employee ID to 10 characters with zeros 
   // Use Simple String append rather than StringBuilder
   // should check defaultExtension not null here etc 

    string tmpImageName = vID.ToString("D10") + defaultExtension;
    return tmpImageName;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900