开发者

Datalist control : binding with ADO.NET objects

开发者 https://www.devze.com 2023-03-17 11:35 出处:网络
Here\'s the code: private void ShowPossiblePurchases(string CategoryName) { string selectSQL = \"SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()\";

Here's the code:

private void ShowPossiblePurchases(string CategoryName)
{

    string selectSQL = "SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()";
    SqlCommand cmd = new SqlCommand(selectSQL, connection);
    cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
    SqlDataReader reader;

    DataSet myDataSet = new DataSet();
    myDataSet.Tables.Add("Products");


    myDataSet.Tables["Products"].Columns.Add("ProductID");
    myDataSet.Tables["Products"].Columns.Add("CategoryID");
    myDataSet.Tables["Products"].Columns.Add("ProductName");
    myDataSet.Tables["Products"].Columns.Add("Price");

    DataList DataList1 = (DataList)lgnView.FindControl("DataList1");

    try
    {
        connection.Open();
        reader = cmd.ExecuteReader();

        while (reader.Read())
        {

            DataRow rowNew = myDataSet.Tables["Products"].NewRow();
            rowNew["ProductID"] = reader["ProductID"];
            rowNew["CategoryID"] = reader["CategoryID"];
            rowNew["开发者_Python百科ProductName"] = reader["ProductName"];
            rowNew["Price"] = reader["Price"];
            myDataSet.Tables["Products"].Rows.Add(rowNew);
        }

        DataList1.DataSource = myDataSet.Tables["Products"];
        DataList1.DataBind();
    }
    catch(Exception ex)
    {
        Label lblError = (Label)lgnView.FindControl("lblError");
        lblError.Text = ex.Message;
    }
    finally
    {
        connection.Close();
    }
}

When i run it, nothing happens. What am i doing wrong?


I've re-created the above code on my side and it's working perfectly. Because you didn't provide the source for the DataList I strongly believe that the problem lies in you not properly binding the data in the DataList.

Here's an example on how this can be done:

<asp:DataList ID="DataList1" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <td>
                    ProductID
                </td>
                <td>
                    CategoryID
                </td>
                <td>
                    ProductName
                </td>
                <td>
                    Price
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table>
            <tr>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "ProductID")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "CategoryID")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "ProductName")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "Price")%>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

Additionally here is the MSDN DataList article.Here you can find a good example on how to bind data to the DataList.

Just a note Any specific reason why you're trying to bind to a DataaList? The code in your question can easily be bound to a gridview like so:

Web.config

<connectionStrings>
    <add name="conn" connectionString="your connection string"/>
</connectionStrings>

Source

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

Code behind

private void ShowPossiblePurchases(string categoryName)
{
    if (!String.IsNullOrEmpty(categoryName))
    {
        try
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection connection = new SqlConnection(connString);
            string commandText = "SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()";
            using (connection)
            {
                using (SqlCommand command = new SqlCommand(commandText, connection))
                {
                    command.Parameters.AddWithValue("@CategoryName", categoryName);
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable table = new DataTable();
                    adapter.Fill(table);
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
            connection.Close();
        }
        catch (Exception ex)
        {
            Label lblError = (Label)Page.FindControl("lblError");
            lblError.Text = ex.Message;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号