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 = "Products"를 추가하려고 시도합니다. – 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> 

MSDN DataList article.Here 당신이 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 [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; 
     } 
    } 
} 
뒤에
관련 문제