Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Databinding in Combobox using C#
WhatsApp
Satyapriya Nayak
12y
53.7
k
0
1
25
Blog
Databind.rar
In this blog we will know how to bind data from the database to a combobox control in a windows application. Here we will know in three ways how to bind data to a combobox. Out of three methods, we use DataAdapter in two methods and DataReader in other one.
Table structure
Create a table named as student in ms-access having column names as below
sid text,sname text,smarks number,saddress text,year text
Add an App.config file to the application
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\EMP.mdb" />
</appSettings>
</configuration>
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.OleDb;
namespace
Dynamically_bind_data_to_combobox_from_database
{
public
partial
class
Form1
:
Form
{
string
ConnectionString = System.Configuration.
ConfigurationSettings
.AppSettings[
"dsn"
];
OleDbCommand
com;
OleDbDataAdapter
oledbda;
DataSet
ds;
string
str;
DataTable
dt;
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender,
EventArgs
e)
{
OleDbConnection
con =
new
OleDbConnection
(ConnectionString);
con.Open();
str =
"select * from student"
;
com =
new
OleDbCommand
(str, con);
oledbda =
new
OleDbDataAdapter
(com);
ds =
new
DataSet
();
oledbda.Fill(ds,
"student"
);
dt = ds.Tables[
"student"
];
int
i;
for
(i = 0; i <= dt.Rows.Count - 1; i++)
{
comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);
}
con.Close();
}
private
void
button2_Click(
object
sender,
EventArgs
e)
{
OleDbConnection
con =
new
OleDbConnection
(ConnectionString);
con.Open();
str =
"select * from student"
;
com =
new
OleDbCommand
(str, con);
oledbda =
new
OleDbDataAdapter
(com);
ds =
new
DataSet
();
oledbda.Fill(ds,
"student"
);
comboBox2.DataSource = ds.Tables[
"student"
];
comboBox2.DisplayMember =
"sname"
;
con.Close();
}
private
void
button3_Click(
object
sender,
EventArgs
e)
{
OleDbConnection
con =
new
OleDbConnection
(ConnectionString);
con.Open();
str =
"select * from student"
;
com =
new
OleDbCommand
(str, con);
OleDbDataReader
reader = com.ExecuteReader();
while
(reader.Read())
{
comboBox3.Items.Add(reader[
"smarks"
]);
}
reader.Close();
con.Close();
}
private
void
Form1_Load(
object
sender,
EventArgs
e)
{
comboBox1.Text=
"Plz Click the button"
;
comboBox2.Text=
"Plz Click the button"
;
comboBox3.Text=
"Plz Click the button"
;
}
}
}
Output
Thanks for reading
Up Next
Bulk Copy In SQL Server Using C#
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Membership not found