I have this code in Generic Handler to connect to database and retrieve the image from table,
But when user logging on will choose the database, for example:
my database is: SH001
user: username
Password: password
I save the database in Session["db"]
when logging in
and when the user go to products page and searching for products...it list the products with their images...
but since I changed the connection from:
System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=SH001;User ID=userid;Password=password");
to:
System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=" + HttpContext.Current.Session["db"] + ";User ID=userid;Password=password");
it starts to give me an error when retreive the image in products page
Object reference not set to an instance of an object.
this is the code in the Generic Handler:
public class image : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection("Data Source=DVR;DataBase=" + HttpContext.Current.Session["db"] + ";User ID=userid;Password=pass");
string image = context.Request.QueryString["ID"].ToString();
SqlCommand com = new SqlCommand("Select Image from Products where ID = '" + image + "'", Con);
Con.Open();
SqlDataReader Reader = com.ExecuteReader();
while (Reader.Read())
{
if (Reader.GetValue(0) != DBNull.Value)
{
context.Response.BinaryWrite((byte[])Reader["Image"]);
}
}
Con.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}