ASP.NET - How to bind image to datalist conteol which(image) is stored as binary data in mysql table

Asked By Rohini Kuchadi on 18-Mar-13 05:52 AM
Hi 
   
      In my application i wants to bind data to datalist control,by retrieving data from mysql database.projectname,image,plan etc.. are fields which i has to select from mysql database and bind it to the datalist control.Image is stored in binary format in mysql database.


Here is the source code of the page which i have designed.


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">


<h1>Project <span>Gallery</span></h1>         
    
             <div class="project_gall">  
                 <asp:DataList ID="DataList1" runat="server" CellPadding="6" CellSpacing="6" 
                     RepeatColumns="3" onitemcommand="DataList1_ItemCommand">
                  <ItemTemplate> 
                  <table  style="border:none" >
                        <tr><td valign="top">
                        <asp:Label Font-Bold="true" Font-Size="12px" ID="lblprojectname" runat="server"  Text='<%# DataBinder.Eval (Container.DataItem, "projectname") %>'></asp:Label>
                        </td></tr>
                       
                        <tr><td>
                        <img src='<%# DataBinder.Eval(Container.DataItem, "images") %>' alt="" style="height:100px;width:100px;border:1px solid gray;"/>
                         <%--<asp:Image ID="img" runat="server" ImageUrl='<%# Eval("images") %>' />--%>
                        </td></tr>
                          
                        <tr><td valign="top">
                        <asp:Label Font-Bold="true" Font-Size="12px" ID="lbllocality" runat="server"  Text='<%# DataBinder.Eval (Container.DataItem, "locality") %>'></asp:Label>
                        </td></tr>
                        <tr><td valign="top">
                        <asp:Label Font-Bold="true" Font-Size="12px" ID="lblsaleprice" runat="server"  Text='<%# DataBinder.Eval (Container.DataItem, "saleprice") %>'></asp:Label>
                        </td></tr>
                        <tr><td valign="top">
                        <asp:Label Font-Bold="true" Font-Size="12px" ID="lblplans" runat="server"  Text='<%# DataBinder.Eval (Container.DataItem, "plans") %>'></asp:Label>
                        </td></tr> </table>
</ItemTemplate></asp:DataList>
</asp:Container>
--------------------------------------------------------------------------------------------------------------------------------

Here is the .aspx.cs code which i have written.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;
using System.IO;


public partial class home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            BindDataList();
        }


    }


    public void BindDataList()
    {
      
        // retrieved from the connectionStrings section of web.config  
        string jsrcon = ConfigurationManager.ConnectionStrings["jsrconnection"].ConnectionString;


        


        // sql connection object  
        MySqlConnection mySqljsrConnection = new MySqlConnection(jsrcon);


        // sql command object initialized with select command text  
        MySqlCommand mySqlCommand = new MySqlCommand("select projectname, images, locality, saleprice, plans from transmaster", mySqljsrConnection);


        // check the connection state and open it accordingly.  
        if (mySqljsrConnection.State == ConnectionState.Closed)
            mySqljsrConnection.Open();


        // Sql datareader object to read the stream of rows from SQL Server Database  
        MySqlDataReader myDataReader = mySqlCommand.ExecuteReader();


        // Pass the Sql DataReader object to the DataSource property  
        // of DataList control to render the list of items.  
        DataList1.DataSource = myDataReader;
        DataList1.DataBind();


        // close the Sql DataReader object  
        myDataReader.Close();


        // check the connection state and close it accordingly.  
        if (mySqljsrConnection.State == ConnectionState.Open)
            mySqljsrConnection.Close();
    }
}

Kindly help me how to bind binary image to the datalist control by retrieving it from mysql database.


Thanks in Advance.