Home All Groups Group Topic Archive Search About

Trouble with the DataList Control

Author
23 Feb 2006 4:32 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
24 Feb 2006 6:13 AM
Nathan Sokalski
Call conn.Close() after the Fill() method. When you are using the Fill()
method to obtain the records, it is not necessary to call the Open() method.
If you would like to see a working example that I used, let me know. Good
Luck!
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quoteHide quote
"Robert Sheppard" <shep***@cox.net> wrote in message
news:%23p%23AtXJOGHA.1028@TK2MSFTNGP11.phx.gbl...
>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.
>
>