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 AroraRank #327 · 7.6K reputationPosted Jul 27, 2024, 4:38 AM EDT
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.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jul 25, 2024, 2:48 AM EDT
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.
// Assuming you have a connection established (conn) and textboxes (txtAcctNumber, txtClubNumber, txtAmount) string query = "SELECT Image FROM ImagesTable WHERE 1=1"; List conditions = new List(); if (!string.IsNullOrEmpty(txtAcctNumber.Text)) { conditions.Add("AcctNumber = @AcctNumber"); } if (!string.IsNullOrEmpty(txtClubNumber.Text)) { conditions.Add("ClubNumber = @ClubNumber"); } if (!string.IsNullOrEmpty(txtAmount.Text)) { conditions.Add("Amount = @Amount"); } if (conditions.Count > 0) { query += " AND " + string.Join(" AND ", conditions); } // Execute the query using parameters SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue("@AcctNumber", txtAcctNumber.Text); cmd.Parameters.AddWithValue("@ClubNumber", txtClubNumber.Text); cmd.Parameters.AddWithValue("@Amount", txtAmount.Text); // Execute the command and retrieve imagesBy 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.
Abhishek YadavRank #217 · 11.3K reputationPosted Jul 28, 2024, 1:46 AM EDT
string query = "SELECT Image FROM ImagesTable WHERE 1=1"; List conditions = new List(); if (!string.IsNullOrEmpty(txtAcctNumber.Text)) { conditions.Add("AcctNumber = @AcctNumber"); } if (!string.IsNullOrEmpty(txtClubNumber.Text)) { conditions.Add("ClubNumber = @ClubNumber"); } if (!string.IsNullOrEmpty(txtAmount.Text)) { conditions.Add("Amount = @Amount"); } if (conditions.Count > 0) { query += " AND " + string.Join(" AND ", conditions); } // Execute the query using parameters SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue("@AcctNumber", txtAcctNumber.Text); cmd.Parameters.AddWithValue("@ClubNumber", txtClubNumber.Text); cmd.Parameters.AddWithValue("@Amount", txtAmount.Text);Ivonne AspilcuetaRank #4,887 · 320 reputationPosted Jul 29, 2024, 1:17 PM EDT
Thank you everyone!
@Gurpreet Arora Flawless! :)