0% found this document useful (0 votes)
3 views

ADO.Net architechture

Uploaded by

afganraza007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ADO.Net architechture

Uploaded by

afganraza007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is a ComboBox Controller?

A ComboBox is a graphical user interface (GUI) control that allows users to select a single item
from a drop-down list. It can also allow users to enter a custom value if the DropDownStyle is
set to DropDown.

In Windows Forms, the ComboBox provides features like:

 Displaying a list of items.


 Binding data dynamically from a database.
 Allowing users to select an item or type custom text.

Filling a ComboBox with Data from TBLDEPT Using SQL Query

SQL Query to Fetch Data:

To populate the ComboBox with department names (DeptName) from TBLDEPT:

SELECT DeptID, DeptName FROM TBLDEPT;


C# Code to Fill the ComboBox Using ADO.NET

Assume you have a ComboBox named cmbDept.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

public class ComboBoxFiller


{
private static string connectionString = "YourConnectionStringHere";

public void FillComboBox(ComboBox cmbDept)


{
try
{
using (SqlConnection connection = new
SqlConnection(connectionString))
{
connection.Open();

string query = "SELECT DeptID, DeptName FROM TBLDEPT";

using (SqlCommand command = new SqlCommand(query, connection))


{
using (SqlDataAdapter adapter = new
SqlDataAdapter(command))
{
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);

// Bind data to ComboBox


cmbDept.DataSource = dataTable;
cmbDept.DisplayMember = "DeptName"; // Display this in
the ComboBox
cmbDept.ValueMember = "DeptID"; // Value associated
with each item
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

Key Points in the Code:

1. DataSource: Binds the data retrieved from the database to the ComboBox.
2. DisplayMember: Specifies the column (DeptName) to display in the ComboBox.
3. ValueMember: Specifies the column (DeptID) that acts as the underlying value for each item.

How to Use This Code

1. Create an instance of the ComboBoxFiller class.


2. Call the FillComboBox method, passing the ComboBox control (cmbDept) as an argument.

private void Form1_Load(object sender, EventArgs e)


{
ComboBoxFiller filler = new ComboBoxFiller();
filler.FillComboBox(cmbDept);
}

Result:

When the form loads:

 The cmbDept ComboBox will be filled with department names from the TBLDEPT table.
 Each item in the ComboBox will have:
o Displayed text: DeptName.
o Associated value: DeptID.
Let me know if you need further clarification or enhancements!

You might also like