Hello,
Is there a better way to retrieve images from database when you have different input options? Per example: I have 3 textbox: acct Numer, Club Number and Amount , If I enter data on any of those 3 boxes and click to retrieve Images, it will retireve the image.
I have an Image Handler but it only let me retrieve images with one input not several or different inputs. I have an error object not found because I am not fill in the othet textboxes.
Sorry, I can't upload my code, it does not let me...

Gurpreet AroraPosted Jul 27, 2024, 8:38 AM
Step 1: Design the SQL Query
First, design a stored procedure or a dynamic SQL query that can handle multiple optional parameters. Here’s an example of a stored procedure that handles optional parameters:
CREATE PROCEDURE GetImages
@AcctNumber NVARCHAR(50) = NULL,
@ClubNumber NVARCHAR(50) = NULL,
@Amount DECIMAL(18,2) = NULL
AS
BEGIN
SELECT *
FROM Images
WHERE (@AcctNumber IS NULL OR AcctNumber = @AcctNumber)
AND (@ClubNumber IS NULL OR ClubNumber = @ClubNumber)
AND (@Amount IS NULL OR Amount = @Amount)
END
This stored procedure will return images based on the provided parameters. If a parameter is
NULL, it will not filter by that criterion.Step 2: Update the Image Handler in ASP.NET
Modify your image handler to accept multiple parameters and pass them to the stored procedure. Below is an example of an HTTP handler in ASP.NET:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string acctNumber = context.Request.QueryString["acctNumber"];
string clubNumber = context.Request.QueryString["clubNumber"];
string amountStr = context.Request.QueryString["amount"];
decimal? amount = string.IsNullOrEmpty(amountStr) ? (decimal?)null : Convert.ToDecimal(amountStr);
using (SqlConnection conn = new SqlConnection("YourConnectionString"))
{
using (SqlCommand cmd = new SqlCommand("GetImages", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@AcctNumber", string.IsNullOrEmpty(acctNumber) ? (object)DBNull.Value : acctNumber);
cmd.Parameters.AddWithValue("@ClubNumber", string.IsNullOrEmpty(clubNumber) ? (object)DBNull.Value : clubNumber);
cmd.Parameters.AddWithValue("@Amount", amount.HasValue ? (object)amount.Value : DBNull.Value);
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite((byte[])reader["ImageData"]);
}
else
{
context.Response.StatusCode = 404;
}
}
}
}
}
public bool IsReusable
{
get { return false; }
}
}
Step 3: Modify the Front-End
Ensure your front-end form passes the parameters correctly to the image handler. Here's an example of how you might set this up with an HTML form and JavaScript:
This setup ensures that you can retrieve images based on any combination of the three input fields. By constructing a flexible query and handling optional parameters properly, you avoid errors related to missing inputs.
Ivonne AspilcuetaPosted Jul 29, 2024, 5:17 PM
Thank you everyone!
@Gurpreet Arora Flawless! :)
Abhishek YadavPosted Jul 28, 2024, 5:46 AM
Jayraj ChhayaPosted Jul 25, 2024, 6:48 AM
To retrieve images from a database with multiple input options in a .NET application, you can dynamically construct a SQL query based on the filled textboxes. You can use parameters to handle varying input scenarios efficiently.
By dynamically building the SQL query based on the filled textboxes and using parameters, you can retrieve images from the database efficiently, handling different input scenarios without errors.