Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

How to bind datas from sql database to datalist control.

I have a datalist control and inside that it has a asp image control.
In my database i have saved the Image name only. and i need to show the images in the datalist control.

I have used the code below mentioned.

ASP.NET
<asp:Image ID="imgItem" runat="server" Height="75px" ImageUrl='../../Content/images/thumbs/sub/<%# Eval("Sub_Item_Img") %>' Width="75px" />


Is there any way to show the image using asp image control. ?

When i used this code it didn't works. please help me....

Thanks

Dile.
Posted
Updated 9-Aug-12 21:58pm
v4

XML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
   Width="100%" BorderColor="#336699" BorderStyle="Solid" BorderWidth="2px">

   <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>' Font-Bold="True"
         Font-Size="10pt" ForeColor="#336699" Width="100%" />
      <br />
      <asp:Image ID="Image1" runat="server"
         ImageUrl='<%# "GetImage.aspx?id=" + Eval("ProductID") %>' />
   </ItemTemplate>
   <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"  />
</asp:DataList>
 
Share this answer
 
XML
<div id="our_product_page_right_product_Product">
                    <asp:DataList ID="DataList1" runat="server" DataKeyField="p_id" RepeatDirection="Horizontal"
                        RepeatColumns="5" EnableViewState="true">
                        <ItemTemplate>
                            <div class="ourproductPAGE_product">
                                <!--Image -->
                                <asp:ImageButton CssClass="ourproductPAGE_productImage" ID="ImageButton1" ImageUrl='<%# Eval("p_image") %>'
                                    runat="server" PostBackUrl='<%# "ProductDetail.aspx?id=" +Eval("p_id") %>' />
                                <div class="ourproductPAGE_productInfo">
                                    <!--Name -->
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("p_name") %>' /><br />
                                    <!--Cost -->
                                    Cost:&nbsp;&#8377;&nbsp;<asp:Label ID="Label2" runat="server" Text='<%# Eval("p_cost") %>' />

                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:DataList>
                </div>
                <div id="our_product_page_right_product_Previous-next">
                    <asp:Button ID="Prev" runat="server" Text="<<" OnClick="cmdPrev_Click"/>
                    <asp:Label ID="PageNo" runat="server" Text="Label"></asp:Label>
                    <asp:Button ID="Next" runat="server" Text=">>" OnClick="cmdNext_Click"/>
                </div>


<in demo.cs="">

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Our_Product : System.Web.UI.Page
{

    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getdata();
        }
    }
    public int CurrentPage
    {
        get
        {
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0;
            else
                return (int)o;
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }
    public void getdata()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Health_RazConnectionString"].ConnectionString);
        da = new SqlDataAdapter("select * from Organic_suppliment", con);
        con.Open();
        ds = new DataSet();
        da.Fill(ds, "Organic_suppliment");
        PagedDataSource objpds = new PagedDataSource();
        DataView dv = ds.Tables["Organic_suppliment"].DefaultView;
        objpds.DataSource = dv;
        objpds.AllowPaging = true;
        objpds.PageSize = 15;
        objpds.CurrentPageIndex = CurrentPage;
        PageNo.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objpds.PageCount.ToString();
        ArrayList mytotalpages = new ArrayList();
        for (int a = 1; a <= objpds.PageCount; a++)
        {
            mytotalpages.Add(a);
        }
        Prev.Enabled = !objpds.IsFirstPage;
        Next.Enabled = !objpds.IsLastPage;
        DataList1.DataSource = objpds;
        DataList1.DataBind();
    }
    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        CurrentPage -= 1;
        getdata();
    }
    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        CurrentPage += 1;
        getdata();
    }
}
 
Share this answer
 
v2
hiii

just change your code to

<asp:image width="100" id="Image1" imageurl="<%# Bind(" sub_item_img",="" "..="" ..="" content="" images="" thumbs="" sub="" {0}")="" %&gt;"="" runat="server" xmlns:asp="#unknown">
 
Share this answer
 
XML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
   Width="100%" BorderColor="#336699" BorderStyle="Solid" BorderWidth="2px">

   <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>' Font-Bold="True"
         Font-Size="10pt" ForeColor="#336699" Width="100%" />
      <br />
      <asp:Image ID="Image1" runat="server"
         ImageUrl='<%# "Map image here ; + Eval("ProductID") %>' />
   </ItemTemplate>
   <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"  />
</asp:DataList>


this will work fine and well tested please make sure your path is corrrect or in valid format like ../Images/img.jpg this format not in c://Images/img.jpg  
please check and if problem persist than give me your review
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900