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:
- Open Microsoft Visual Studio, create a new windows form application and do the form just like this.
- 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)
{
try
{
//opening connection
con.Open();
//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.
da.Fill(dt);
//add the data source to display the data in the ListView
dtglist.DataSource = dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
da.Dispose();
con.Close();
}- Set up the connection between SQL Server and C#.net. 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 + "'"; displayDTG(query);
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
| ABOUT PROJECT | PROJECT DETAILS |
|---|---|
| Project Name : | Searching Records Using a ComboBox with SQL Server |
| Project Platform : | C# |
| Programming Language Used: | C# Programming Language |
| Developer Name : | itsourcecode.com |
| IDE Tool (Recommended): | Visual Studio 2019 |
| Project Type : | Desktop Application |
| Database: | MySQL Database |
| Upload Date and Time: | June 24, 2016 – 8:20 am |
Frequently Asked Questions
How does this C# ComboBox example work?
Populating ComboBox items (from array, List
What .NET and SQL Server versions does this project require?
Most projects in this batch use C# WinForms on .NET Framework 4.5+ (the dominant stack for tutorial sites) with SQL Server 2012 Express or higher. A few newer projects use .NET 6/7/8. To run: install Visual Studio 2019 / 2022 (Community edition is free), install SQL Server Express + SSMS, open the .sln file, build, run.
How do I set up the database for this C# project?
Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance (e.g. localhost\SQLEXPRESS). Right-click Databases > Restore Database OR > New Database then import the included .sql script. Update the connection string in App.config (or in code-behind) with your server name + credentials. Rebuild and run.
Can I use this C# project for a BSIT capstone or thesis?
Yes, but extend it. A bare CRUD form is too narrow for full capstone scope. Add: role-based access (admin/staff/customer login redirect), Crystal Reports or RDLC reports, dashboard with Chart controls, audit log, multi-branch support. Pair with Chapter 1-5 documentation matching your panel’s rubric.
Why am I getting ‘connection error’ or ‘object reference not set’?
Three common C# issues: (1) Connection error: SQL Server isn’t running OR connection string in App.config has wrong server name. Open SQL Server Configuration Manager + verify SQL Server (SQLEXPRESS) service is running. (2) NullReferenceException: a control reference or DB column returned NULL, add a check or use ?? operator. (3) Build error ‘The type or namespace could not be found’: missing assembly reference, add via Project > Add Reference.
Where can I find more C# projects with source code?
Browse the C# Projects hub for the full library. For other .NET stacks see VB.NET Projects (300+ Windows Forms systems). For ASP.NET WebForms see ASP.NET Projects. For BSIT capstone idea lists see 150 Best Capstone Project Ideas.


