2014-02-18 2 views
1

내 콤보 상자가 SQL- 특성 "TimeBlock"의 값을 표시하지 않고 대신 System.Data.DataRow 5 번을 표시합니다. 내 코드에 어떤 문제가 있습니까?콤보 상자에 System.Data.DataRow (MVC)가 표시됩니다.

코드 :

//DAL: 

    public class DAL{ 

    string ConnectionString = "server=ICSSQL13\\Grupp28,1528; Trusted_Connection=yes; database=Yoloswag"; 

    public DataTable StoreSqlDataInComboBoxTP() 
      { 

       SqlConnection Conn = new SqlConnection(ConnectionString); 

       Conn.Open(); 

       string StoreSqlDataInComboBoxTP = "SELECT TimeBlock FROM TimePeriod GROUP BY TimeBlock"; 

       SqlCommand Cmd = new SqlCommand(StoreSqlDataInComboBoxTP, Conn); 

       SqlDataAdapter Adapter = new SqlDataAdapter(Cmd); 

       DataSet DSet = new DataSet(); 

       Adapter.Fill(DSet); 

       Adapter.Dispose(); 
       Cmd.Dispose(); 
       Conn.Close(); 
       Conn.Close(); 

       return DSet.Tables[0]; 
      } 
    } 

    //Controller: 

    public class Controller 
    { 
    DAL Dal = new DAL(); 

    public DataTable storesqldataincomboboxtp() 
     { 
      return Dal.StoreSqlDataInComboBoxTP(); 
     } 
    } 

//View: 
public partial class Booking : Form 
    { 
     Controller controller = new Controller(); 
     DataTable DTable = new DataTable(); 
     DataSet DSet = new DataSet(); 

     //Ignore string UserName 
     public Booking(string UserName){ 
      DTable = controller.storesqldataincomboboxtp(); 

      if (DTable.Rows.Count > 0) 
      { 
       for (int i = 0; i < DTable.Rows.Count; i++) 
       { 
        CBTime.Items.Add(DTable.Rows[i].ToString()); 
       } 
      } 
     } 
    } 

대신 내가 "TimeBlock"에 저장되어있는 것을 표시 할 5 System.Data.DataRow. "TimePeriod GROUP BY TimeBlock에서 TimeBlock을 선택하는 것은"보여줍니다 "08-00 10:00 -" "10시부터 12시까지" "12시에서 14시까지" "14시 - 16 : 00 " "16:00 - 18:00 "

어떻게 해결할 수 있습니까? 당신이 CBTime에 추가()를 호출 할 때

감사

답변

1

당신은 필드 레벨에 점점되지 않습니다. 조건부 검사에서 테이블에 행이있는 것으로 확인되면 다음과 같이 작동합니다.

foreach (DataRow dRow in DTable.Rows) 
{ 
    CBTime.Items.Add(dRow["TimeBlock"]); 
}