sql-serverasp.net-mvccomboboxdatatablethree-tier

ComboBox Shows System.Data.DataRow (MVC)


My Combobox does not show me the values in my SQL-Attribute "TimeBlock", instead it shows System.Data.DataRow 5 Times. What is wrong with my code?

Code:

    //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());
                }
            }
         }
     }

Instead of the 5 System.Data.DataRow I want to show what is stored in "TimeBlock". "SELECT TimeBlock From TimePeriod GROUP BY TimeBlock" shows: "08-00 - 10:00" "10:00 - 12:00" "12:00 - 14:00" "14:00 - 16:00" "16:00 - 18:00"

How can i solve this?

Thanks


Solution

  • You are not getting to the Field level when you are calling the Add() on CBTime. Something like this within your conditional checking that your table has rows would work:

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