以下是代码:
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["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();
}
}
当我运行它时,没有任何反应。我做错了什么?
答案 0 :(得分:0)
我已经在我身边重新创建了上面的代码,它运行得很好。 因为您没有提供DataList的源代码,所以我坚信问题在于您没有正确绑定DataList中的数据。
以下是如何做到这一点的示例:
<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>
此外还有MSDN DataList文章。在这里你可以找到一个关于如何将数据绑定到DataList的好例子。
只是一个注释 您尝试绑定到DataaList的任何特定原因? 您问题中的代码可以很容易地绑定到gridview,如下所示:
的Web.config
<connectionStrings>
<add name="conn" connectionString="your connection string"/>
</connectionStrings>
来源
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
背后的代码
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;
}
}
}