0
votes

Hi I am trying to retrieve pProductImage that contains ie. ~/Images/pic1.jpg (images url) from products table and use the pProductId as a identifier to display different images according to the Id.

this is my cataglogue.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OleDb;


public partial class Catalogue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ImageId = Request.QueryString["Img"];

        string sqlText = "SELECT pProductImage FROM Products WHERE pProductId = " +ImageId;

        OleDbConnection mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString);
        OleDbCommand cmd = new OleDbCommand(sqlText, mDB);

        mDB.Open();
        OleDbDataReader rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
            Response.BinaryWrite((byte[])rdr["pProductImage"]);
        }
        mDB.Close();

    }
}

and in my cataglogue.aspx (the image to display in the image control)

<div>

    <img src="Catalogue.aspx?pProductId=2" alt="" />
    </div>
1
what is there in the database image or image path? - शेखर
the image path of my image - Malcolm Nathaniel Ng
why are you using Response.BinaryWrite((byte[])rdr["pProductImage"]); - शेखर
do you want to create a handler type of functionality. where you just pass the id and it should return the image? - शेखर

1 Answers

0
votes

If you are storing image url in your database table then you need to treat it as a string data - for example

in code-behind:

protected string GetProductImgUrl(int productId)
{
   var sqlText = "SELECT pProductImage FROM Products WHERE pProductId = " + productId.ToString();
   using(var mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString))
   {
       mDB.Open();
       var cmd = new OleDbCommand(sqlText, mDB);
       using (var rdr = cmd.ExecuteReader())
       {
          if (rdr.Read())
          {
            return ResolveUrl((rdr["pProductImage"]).ToString());
          }
       }
   }
   return ResolveUrl("~/images/NotAvailable.png");     // Not available image
}

And in mark-up

<div>
    <img src='<%= GetProductImgUrl(2) %>' alt="" />
</div>