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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mageshwaran RPosted Nov 14, 2019, 11:48 AM
Kirtan PatelPosted Sep 2, 2009, 7:15 AM
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());
}
}
Naveen KumarPosted Sep 2, 2009, 6:41 AM
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.
Niradhip ChakrabortyPosted Sep 2, 2009, 6:29 AM
The syntax will be same for you. all you need to do is change the sql statement and combox Id
roy himanshuPosted Sep 2, 2009, 6:12 AM
iam not able to understand.plz give me clearly
thanx
Niradhip ChakrabortyPosted Sep 2, 2009, 6:10 AM
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;
Roei BarPosted Sep 2, 2009, 5:53 AM