<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" />
<asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src="GridViewImage123.ashx?autoid=<%# Eval("Id").ToString() %>&tableName=<%# Eval("TableName").ToString() %>"
width="150" height="100" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
While combining all the data, add one more column table name also
i.e in
string qry1 = "Select ProductID,ProductName,ProductDescription,ProductPrice,ProductImage,'tblImage' as TableName from tblImage";
string qry2 = "Select ProductID,ProductName,ProductDescription,ProductPrice,ProductImage,'tblRatnams' as TableName from tblRatnams";
string qry3 = "Select ProductID,ProductName,ProductDescription,ProductPrice,ProductImage,'tblRudrakshas' as TableName from tblRudrakshas";
public class GridViewImage123 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["autoId"] == null) return;
string constr = "Data Source=.\\SQLEXPRESS;AttachDbFILEName=|DataDirectory|\\ImageDB.mdf;integrated security=true;user instance=true;";
string productId = context.Request.QueryString["productId"];
string tableName = context.Request.QueryString["tableName"];
using (SqlConnection conn = new SqlConnection(constr))
{
string query = "SELECT ProductImage FROM " + tableName + " WHERE ProductID =" + productId;
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
{
reader.Read();
context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("ProductImage")]);
reader.Close();
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}