Home All Groups Group Topic Archive Search About
Author
23 Feb 2006 4:34 PM
Robert Sheppard
I have multiple DataList controls on the same page and I want to bind each
of them to different datasets.

Is this possible?

I am currently using the following syntax to bind my first
dataset.

private void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string sql = "select * from table1 where id = 1";
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        da.Fill(ds, "data1");
        DataList1.DataSource = ds;
        DataList1.DataMember = "data1";
        DataList1.DataBind();
    }
}

<asp:datalist id="DataList1", runat="server" >
    <itemtemplate>
        <table>
            <tr>
                <td><asp:Label id="Label1" runat="server" text='<%#
DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td>
                <td><asp:Label id="Label2" runat="server" text='<%#
DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td>
            </tr>
        </table>
    </itemtemplate>
</asp:datalist>



I would like to do the following....


private void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string sql = "select Name, Value from table1 where id = 1";
        conn.Open();
        DataSet ds1 = new DataSet();
        SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
        da1.Fill(ds1, "data1");
        DataList1.DataSource = ds1;
        DataList1.DataMember = "data1";
        DataList1.DataBind();

        sql = "select City, State from table2 where id = 1";
        DataSet ds2 = new DataSet();
        SqlDataAdapter da2 = new SqlDataAdapter(sql, conn);
        da2.Fill(ds2, "data2");
        DataList2.DataSource = ds2;
        DataList2.DataMember = "data2";
        DataList2.DataBind();
    }
}

<asp:datalist id="DataList1", runat="server" >
    <itemtemplate>
        <table>
            <tr>
                <td><asp:Label id="Label1" runat="server" text='<%#
DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td>
                <td><asp:Label id="Label2" runat="server" text='<%#
DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td>
            </tr>
        </table>
    </itemtemplate>
</asp:datalist>

<asp:datalist id="DataList2", runat="server" >
    <itemtemplate>
        <table>
            <tr>
                <td><asp:Label id="Label3" runat="server" text='<%#
DataBinder.Eval(Container.DataItem, "City") %>'>Label</asp:Label></td>
                <td><asp:Label id="Label4" runat="server" text='<%#
DataBinder.Eval(Container.DataItem, "State") %>'>Label</asp:Label></td>
            </tr>
        </table>
    </itemtemplate>
</asp:datalist>


The second datalist is always empty.

Author
23 Feb 2006 4:57 PM
Phillip Williams
Step through the code using the debugger; set a break point at this step:
DataList2.DataSource = ds2;

and lookup if ds2 is not nothing and if so lookup the rows.count in the
DataTable.
Show quoteHide quote
"Robert Sheppard" wrote:

> I have multiple DataList controls on the same page and I want to bind each
> of them to different datasets.
>
> Is this possible?
>
> I am currently using the following syntax to bind my first
> dataset.
>
> private void Page_Load(object sender, System.EventArgs e)
> {
>     if (!Page.IsPostBack)
>     {
>         string sql = "select * from table1 where id = 1";
>         conn.Open();
>         DataSet ds = new DataSet();
>         SqlDataAdapter da = new SqlDataAdapter(sql, conn);
>         da.Fill(ds, "data1");
>         DataList1.DataSource = ds;
>         DataList1.DataMember = "data1";
>         DataList1.DataBind();
>     }
> }
>
> <asp:datalist id="DataList1", runat="server" >
>     <itemtemplate>
>         <table>
>             <tr>
>                 <td><asp:Label id="Label1" runat="server" text='<%#
> DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td>
>                 <td><asp:Label id="Label2" runat="server" text='<%#
> DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td>
>             </tr>
>         </table>
>     </itemtemplate>
> </asp:datalist>
>
>
>
> I would like to do the following....
>
>
> private void Page_Load(object sender, System.EventArgs e)
> {
>     if (!Page.IsPostBack)
>     {
>         string sql = "select Name, Value from table1 where id = 1";
>         conn.Open();
>         DataSet ds1 = new DataSet();
>         SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
>         da1.Fill(ds1, "data1");
>         DataList1.DataSource = ds1;
>         DataList1.DataMember = "data1";
>         DataList1.DataBind();
>
>         sql = "select City, State from table2 where id = 1";
>         DataSet ds2 = new DataSet();
>         SqlDataAdapter da2 = new SqlDataAdapter(sql, conn);
>         da2.Fill(ds2, "data2");
>         DataList2.DataSource = ds2;
>         DataList2.DataMember = "data2";
>         DataList2.DataBind();
>     }
> }
>
> <asp:datalist id="DataList1", runat="server" >
>     <itemtemplate>
>         <table>
>             <tr>
>                 <td><asp:Label id="Label1" runat="server" text='<%#
> DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td>
>                 <td><asp:Label id="Label2" runat="server" text='<%#
> DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td>
>             </tr>
>         </table>
>     </itemtemplate>
> </asp:datalist>
>
> <asp:datalist id="DataList2", runat="server" >
>     <itemtemplate>
>         <table>
>             <tr>
>                 <td><asp:Label id="Label3" runat="server" text='<%#
> DataBinder.Eval(Container.DataItem, "City") %>'>Label</asp:Label></td>
>                 <td><asp:Label id="Label4" runat="server" text='<%#
> DataBinder.Eval(Container.DataItem, "State") %>'>Label</asp:Label></td>
>             </tr>
>         </table>
>     </itemtemplate>
> </asp:datalist>
>
>
> The second datalist is always empty.
>
>
>

Bookmark and Share