hi,
How to get data in to ComboBox from Data Base when form is load?Iam working on windows application in C#.so i want C# codeing?plz help me.
is it possible?PLZ Tell me.
Himanshu
hi,
How to get data in to ComboBox from Data Base when form is load?Iam working on windows application in C#.so i want C# codeing?plz help me.
is it possible?PLZ Tell me.
Himanshu
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Sep 2, 2009, 3:15 AM EDT
you can do it Like Below in Which i Have Taken One Table Users ( Username , Password)
Now if i want to Fill ComboBox With Usernames then I can Code it Like Below
dont forget to mark "Do you like answer" it will give me some credits :)
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
comm = new SqlCommand("Select Username From Users", con);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader[0].ToString());
}
}
Roei BarRank #1,271 · 1.4K reputationPosted Sep 2, 2009, 1:53 AM EDT
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Sep 2, 2009, 2:10 AM EDT
sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
SqlCommand sqlCmd = new SqlCommand();
SqlDataAdapter adpData = null;
SqlDataReader sdrData = null;
sqlCon.Open();
string strSQL = "SELECT * from BoothTypes";
sqlCmd.CommandText = strSQL;
sdrData = sqlCmd.ExecuteReader();
if (sdrData.HasRows)
{
while (sdrData.Read())
{
cboBoothtype.Items.Add(new ListItem(sdrData["btp_txt_BoothType"].ToString(), sdrData["btp_txt_BoothId"].ToString()));
}
}
sdrBoothTypes.Close();
sqlCon.Close();
sqlCon = null;
roy himanshu98 reputationPosted Sep 2, 2009, 2:12 AM EDT
iam not able to understand.plz give me clearly
thanx
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Sep 2, 2009, 2:29 AM EDT
The syntax will be same for you. all you need to do is change the sql statement and combox Id
Naveen Kumar6 reputationPosted Sep 2, 2009, 2:41 AM EDT
create the connection to the database
sqlconnection con=new sqlconnection(//to do:enter the database credentials);
create the sql query you need to get
string query="select * from table";
create the sqladapter from the query;
dataadapter da=new sqldataadapter(query)
create the dataset
dataset ds=new dataset();;
da.fill(ds);//fills the dataset
the above code is to get the data from the datbase.once the dataset is ready now it is easy to bind with the combobox.
combobox1.datasource=ds;
combobox1.displaymember=ds.tables[0].coloumn0;// the coloumn data which the user need to be viewed.
combobox1.valuemember=ds.tables[0].coloumn1;//the coloumn data which the user need to be used as reference.
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 14, 2019, 6:48 AM EST