Manu Laureys

Manu Laureys

  • NA
  • 40
  • 1.8k

Connecting combobox to datasource

Apr 5 2022 2:30 PM

I have the following code, but get an error "Failed to connect to datasource"

Code:

            OleDbConnection conn = new OleDbConnection();
            // TODO: Modify the connection string and include any
            // additional required properties for your database.
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data source= C:\SerialNumber.accdb";
            try
            {
                conn.Open();
                // Insert code to process data.
                string strCmd = "Select Code, Naam From ProductLine";
                OleDbDataAdapter da = new OleDbDataAdapter(strCmd, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.cboProductLine.DataSource = da;
                this.cboProductLine.DisplayMember = "Naam";
                this.cboProductLine.ValueMember = "Value";

                // make it readonly
                this.cboProductLine.DropDownStyle = ComboBoxStyle.DropDownList;
                //this.cboPruductLine.DisplayMember = "Naam";
                //this.cboPruductLine.ValueMember = "Naam";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to connect to data source: " + ex);
            }
            finally
            {
                conn.Close();
            }
 


Answers (3)