You must Sign In to post a response.
  • Category: ASP.NET

    How To Bind a image without using GridView in asp.net

    Hi Developers ,

    I am having some images in my database table.
    I want to bind a image on Userinterface.
    How i bind the image without Using Gridview.

    I am tried but i did'n get expected output.
    So give me some idea to i am done the task.

    Thanking you
    Paul.S
  • #765624
    Hi,
    try this:
    .ASPX code:
    <div>
    <asp:DataList ID="demoDataList" runat="server">
    <ItemTemplate>
    <div>
    <asp:Image ID="demoImg" runat="server" ImageUrl='<%# Eval("image") %>' width='300' height='300'/>
    </div>
    </ItemTemplate>
    </asp:DataList>
    </div>
    .CS code:
    sqlCon.Open();
    SqlCommand sqlCom = new SqlCommand("select image from table1 where id=1", sqlCon);
    SqlDataAdapter sdataAdapter = new SqlDataAdapter(sqlCom);
    DataTable dataTable = New DataTable();

    sdataAdapter.Fill(dataTable);

    Response.Write(dataTable.Rows[0]["image"].ToString());

    demoDataList.DataSource = dataTable;
    demoDataList.DataBind();

  • #765692
    See the below sample

    Step1
    -----------
    Add a Handelr showIamges.ashx

    Step2
    ----------

    public void ProcessRequest(HttpContext context)
    {
    if (context.Request.QueryString["getID"] != null)
    {
    string str = context.Request.QueryString["getID"].ToString();
    context.Response.ContentType = "image/jpeg";
    Stream strm = ShowEmpImage(str);
    byte[] buffer = new byte[4096];
    int byteSeq = strm.Read(buffer, 0, 4096);
    while (byteSeq > 0)
    {
    context.Response.OutputStream.Write(buffer, 0, byteSeq);
    byteSeq = strm.Read(buffer, 0, 4096);
    }
    }

    }

    public Stream ShowEmpImage(string serialNumber)
    {
    var formats = MultiFormatWriter.SupportedWriters.ToArray();
    var Renderer = typeof(BitmapRenderer);
    string imageData = "URL";
    var writer = new BarcodeWriter
    {
    Format = (BarcodeFormat)formats[3],
    Options = new EncodingOptions
    {
    Height = ht,
    Width = wt
    },
    Renderer = (IBarcodeRenderer<Bitmap>)Activator.CreateInstance(Renderer)
    };

    Bitmap image = writer.Write(imageData);

    byte[] data = imageToByteArray(image);

    return new MemoryStream(data);
    }

    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
    }

  • #765697
    if you don't want to use gridview you can bind images to repeater control as below

    //in aspx
    < asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    < asp:Repeater ID="Repeater1" runat="server">
    < ItemTemplate>
    < div style="display:inline;">
    < img style='border:1px solid #000000' alt="Bind Images from Folder Asp.Net Repeater Control" src='<%# "GetImageDatafromDB.aspx?id=" + System.Convert.ToString(Eval("ProductID")) %>' height="200px"/>
    < /div>
    < /ItemTemplate>
    < /asp:Repeater>
    < /asp:Content>


    //in CS
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    DataTable dt = new DataTable();
    SqlConnection conn = new SqlConnection(connectionString);
    using (conn)
    {
    SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Product", conn);
    ad.Fill(dt);
    }
    Repeater1.DataSource = dt;
    Repeater1.DataBind();
    }
    }

    for more detail see below link
    http://codedisplay.com/bind-images-in-repeater-control-from-sql-server-database-in-asp-net-c-vb-net/

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765704
    Hi Paul,

    If you want to bind images without using datalist controls like gridview, datalist, repeater there is no meaning.

    If you want to bind single image then you can use image control and simply bind your result to that image control, but in your case you want to bind the database result, then you don't have other option you need to choose one of those datalist controls for binding.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments