0

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;
        }
    }
2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by sknake
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;
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.