2011-07-06 4 views
2

オブジェクトコードです:私はそれを実行するとデータリストコントロール:ADO.NETとの結合はここで

private void ShowPossiblePurchases(string CategoryName) 
{ 

    string selectSQL = "SELECT TOP 2 * FROM Menu WHERE [email protected] 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

実際に例外が何を伝えているかをチェックするのではなく、「false」を返します。 – MartW

+0

catchブロックに例外を指定していないのはなぜですか?それは何が起きているかを判断するのに役立つでしょう –

+0

DataList1.DataMember = "製品"の場合にのみ – mslliviu

答えて

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> 

また、ここであなたがDataListコントロールにデータをバインドする方法について良い例を見つけることができますMSDN DataList article.Hereです。

ちょっとメモ DataaListにバインドしようとする具体的な理由は何ですか?

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 [email protected] 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; 
     } 
    } 
} 
背後

のWeb.config

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

ソース

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

コード:あなたの質問で コードは簡単にそうようにGridViewにバインドすることができます

関連する問題