Searching Records Using a ComboBox in C# with SQL Server

Searching Records Using a ComboBox in C# with SQL Server

Searching Records Using a ComboBox in C#

In searching records in the database, we commonly use the TextBox because in a TextBox it is easy for you to type the data that you want to search. While a ComboBox is a bit different from the TextBox because the data that you are going to search is already given and you just have to select whatever data that is on the list of a ComboBox.


For the database of this project just click here.

Let’s begin:


  • Go to the solution explorer and hit the “View Code” to fire the code editor.


  • In the code editor, initialize the SQL class system.
 //initializes new instance 
 SqlConnection con = new SqlConnection();
 SqlDataAdapter da = new SqlDataAdapter();
 SqlCommand cmd = new SqlCommand();
 DataTable dt = new DataTable();
 //declaring variables
 string query;
  • Create a method for displaying data in the dataGridView.
 private void displayDTG(string query)
 //opening connection

 //initialize a new instance of sqlcommand
 cmd = new SqlCommand();
 //set a connection used by this instance of sqlcommand
 cmd.Connection = con;
 //set the sql statement to execute at the data source
 cmd.CommandText = query;
 //initialize a new instance of sqlDataAdapter
 da = new SqlDataAdapter();
 //set the sql statement or stored procedure to execute at the data source
 da.SelectCommand = cmd;
 //initialize a new instance of DataTable
 dt = new DataTable();
 //add or resfresh rows in the certain range in the datatable to match those in the data source.

 //add the data source to display the data in the ListView
 dtglist.DataSource = dt;
 catch(Exception ex)
  • Set up the connection between SQL Server and After that, call a retrieve method that you have created.
 private void Form1_Load(object sender, EventArgs e)
 //set a connection between SQL server and Visual C#
 con.ConnectionString = "Data Source=.\\SQLEXPRESS;Database=dbsubject;trusted_connection=true;";
//set up the query
 query = "Select SUBID as 'Id',SUBJCODE as 'Code',SUBDESC as 'Description',UNIT as 'Unit',YR as 'Year',AY as 'Academic Year' FROM tblsubject";
 displayDTG(query); //calling the method
  • Go back to the design views, double-click the ComboBox and do the following codes for searching records in the database.
 query = "Select SUBID as 'Id',SUBJCODE as 'Code',SUBDESC as 'Description',UNIT as 'Unit',YR as 'Year',AY as 'Academic Year' FROM tblsubject WHERE YR='" + comboBox1.SelectedItem + "'";

For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ :

Email – [email protected]
Mobile No. – 09305235027 – talk’nText

Download Sourcecode

Project Name : Searching Records Using a ComboBox with SQL Server
Project Platform :C#
Programming Language Used:C# Programming Language
Developer Name
IDE Tool (Recommended):Visual Studio 2019
Project Type :Desktop Application
Database:MySQL Database
Upload Date and Time:June 24, 2016 – 8:20 am

Leave a Comment