Member Avatar for mwaqas1990

I am trying to get data from database into ComboBox i don't know where is the mistake in my code. When i run my code which is given below ComboBox show Empty :(

public partial class InstallInfo : Form
    {
        private SqlConnection xSqlConnection;
        private SqlCommand xSqlCommand;
        private SqlDataReader xSqlDataReader;

        public InstallInfo()
        {
            InitializeComponent();
        }

        private void xcbFlatNo_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void InstallInfo_Load(object sender, EventArgs e)
        {
            try
            {
                xSqlConnection = new SqlConnection("Data Source=WaQaS-PC;Initial Catalog=BookingSystem;User ID=sa;Password=123");
                xSqlConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }
            xSqlCommand = new SqlCommand("SELECT Pno FROM Unit1_Table where PStatus = 'Availible'", xSqlConnection);
            xSqlDataReader = xSqlCommand.ExecuteReader();
            bindingSource.DataSource = xSqlDataReader;

            if (xSqlDataReader != null)
            {
                while (xSqlDataReader.Read())
                {
                    xcbFlatNo.Items.Add(bindingSource.DataSource);
                }
            }
            //xcbFlatNo.SelectedIndex = 0;
        }
    }

Try something along these lines:

private void frmSelectEmployee_Load(object sender, EventArgs e)
    {
      const string query = @"Select RepNumber From UserTable (NOLOCK) Order By RepNumber";
      DataTable dt = null;

      comboBox1.BeginUpdate();
      try
      {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
          conn.Open();
          using (SqlCommand cmd = new SqlCommand(query, conn))
          {
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
              dt = new DataTable();
              dt.Load(dr);
            }
          }
          conn.Close();
        }
        //Free up all the sql stuff before we process the data
        for (int i1 = 0; i1 < dt.Rows.Count; i1++)
        {
          comboBox1.Items.Add(dt.Rows[i1]["RepNumber"]);
        }
      }
      finally
      {
        if (dt != null)
        {
          dt.Dispose();
          dt = null;
        }
        comboBox1.EndUpdate();
      }

      loaded = true;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.