How to Load data from Database into Combobox using C#

In this tutorial, I will show you how to fill a Combobox with a Data from Microsoft Access Database. As well as how to search specific record from the database using the data in a combobox. To start building with this project, create a new C# project and design the user interface looks like as shown below. c1 Then save it as “ComboFilter” or whatever you want to name this application. After this step, let’s now create a new database in Microsoft Access named “studentdb”. Then make sure to save this database inside the bin folder of our project called “ComboFilter”. And the table design looks like as shown below. c2 And the figure below show the sample data for this table, and you can follow this also if you wanted to have a uniform output with me. c3 Since we have set our database, let us go back to our project called “ComboFilter”. Then we will add code to it so that it will be functional just like we expect. To do this, And add this “using System.Data.OleDb;” single line of code above “namespace WindowsFormsApplication2” and it should be look like as shown below. c4 The new added code is a .NET Framework Data Provider for OLE DB describes a collection of classes used to access an OLE DB data source in the managed space. Next, we will add a code under the “ public partial class Form1 : Form ”, and here’s the following code:
  1. //declare new variable named dt as New Datatable
  2. DataTable dtable = new DataTable();
  3. //this line of code used to connect to the server and locate the database (studentdb.mdb)
  4. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/studentdb.mdb";
  5. OleDbConnection conn = new OleDbConnection(connection);
After we set our database connection using our code above, let’s now proceed on adding a code on how to load data from the database into Combobox. We only have to do is to add the following code when the form is being loaded. And here’s the following code:
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. //create a new datatable
  4. DataTable dtable = new DataTable();
  5. //create our SQL SELECT statement
  6. string sql = "Select IDNO from tblstudent";
  7. try
  8. {
  9. conn.Open();
  10. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  11. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  12. //we fill the result to dt which declared above as datatable
  13. da.Fill(dtable);
  14. //we add new entry to our datatable manually
  15. //becuase Select Course is not Available in the Database
  16. dtable.Rows.Add("Select IDNO");
  17. //set the combobox datasource
  18. ComboBox1.DataSource = dtable;
  19. //choose the specific field to display
  20. ComboBox1.DisplayMember = "IDNO";
  21. ComboBox1.ValueMember = "IDNO";
  22. //set default selected value
  23. ComboBox1.SelectedValue = "Select IDNO";
  24. }
  25. catch (Exception ex)
  26. {
  27. //this will display some error message if something
  28. //went wrong to our code above during execution
  29. MessageBox.Show(ex.ToString());
  30. }
  31. conn.Close();
  32. }
And if you try to run this application, you can directly observe that the Combobox is not empty and it is filled out with data from the database. And here’s how it looks like. c5 Next, when the user selects a specific ID Number of specific students, and the “Go” button is clicked. Then information about the student will then display in the text box provided. To do this, double click the “Go” button and add the following code:
  1. private void btngo_Click(object sender, EventArgs e)
  2. {
  3. //create a new datatable
  4. DataTable dtable = new DataTable();
  5. //create our SQL SELECT statement
  6. string sql = "Select * from tblstudent WHERE IDNO = '" + ComboBox1 .SelectedValue + "'";
  7. try
  8. {
  9. conn.Open();
  10. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  11. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  12. //we fill the result to dt which declared above as datatable
  13. da.Fill(dtable);
  14.  
  15. //load Data from DataTable to Specific Textboxes
  16. txtFname.Text = dtable.Rows[0][2].ToString();
  17. txtLname.Text = dtable.Rows[0][3].ToString();
  18. txtcourse .Text = dtable.Rows [0][4].ToString();
  19. txtGender.Text = dtable.Rows[0][5].ToString();
  20. txtAddress.Text = dtable.Rows[0][6].ToString();
  21.  
  22.  
  23. }
  24. catch (Exception ex)
  25. {
  26. //this will display some error message if something
  27. //went wrong to our code above during execution
  28. MessageBox.Show(ex.ToString());
  29. }
  30. conn.Close();
  31. }
And when you try to run this application, it will then look like as shown below. c6

Comments

Submitted bylcmon Tue, 07/02/2019 - 18:35

Plz create a same project in VB.NET.

Add new comment