Sunday, December 8, 2019

Lookup Table



Add a comboBox in the form.







 private void FillCoursesCombo()
        {
            if (!ConnectedToDB())
                return;
            //instantiate data adapter and dataset
            OleDbDataAdapter courseAdapter = new OleDbDataAdapter();
            DataSet courseDataSet = new DataSet();
            //instantiate command object
            string strSQL = "SELECT * FROM tblCourses ORDER BY Crs_Description";
            OleDbCommand cmdCourses = new OleDbCommand(strSQL, Conn);
            //set the cmdCourses for the adapter's selectcommand property
            courseAdapter.SelectCommand = cmdCourses;
            //fill the dataset with records from the adapter
            courseAdapter.Fill(courseDataSet);
            //clean up
            courseAdapter.Dispose();
            cmdCourses.Dispose();
            Conn.Close();

            //set the datasourse of the course combobox to the only table from the dataset
            cboCourses.DataSource = courseDataSet.Tables[0];
            //set the course ID as the value member/return value
            cboCourses.ValueMember = "Crs_ID";
            //set the course description as the display member
            cboCourses.DisplayMember = "Crs_Description";
            cboCourses.SelectedIndex = -1;
        }

No comments:

Post a Comment