Combobox problem
-
HI, Am trying to retreive some data into the Visual Studio C#.net (Winforms) combobox from a access database, but when i run it, i get only the first record, Please help me out. Thanks Vibin private void Form2_Load(object sender, System.EventArgs e) { int count=0, b=0; try { con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\JET PRINT LIMITED\\JET PRINT LIMITED.mdb"); da=new OleDbDataAdapter("select * from STOCKDET",con); con.Open(); ds=new DataSet(); da.Fill(ds,"STOCKDET"); comm.Connection=con; comm.CommandType=CommandType.Text; da=new OleDbDataAdapter(comm); dr=ds.Tables [0].Rows [b]; comboBox1.Items.Add (dr[1].ToString()); MessageBox.Show("data into Combobox successfuly!!!"); MessageBox.Show("Connected successfuly!!!"); } catch(Exception k) { MessageBox.Show(k.Message); } }
-
HI, Am trying to retreive some data into the Visual Studio C#.net (Winforms) combobox from a access database, but when i run it, i get only the first record, Please help me out. Thanks Vibin private void Form2_Load(object sender, System.EventArgs e) { int count=0, b=0; try { con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\JET PRINT LIMITED\\JET PRINT LIMITED.mdb"); da=new OleDbDataAdapter("select * from STOCKDET",con); con.Open(); ds=new DataSet(); da.Fill(ds,"STOCKDET"); comm.Connection=con; comm.CommandType=CommandType.Text; da=new OleDbDataAdapter(comm); dr=ds.Tables [0].Rows [b]; comboBox1.Items.Add (dr[1].ToString()); MessageBox.Show("data into Combobox successfuly!!!"); MessageBox.Show("Connected successfuly!!!"); } catch(Exception k) { MessageBox.Show(k.Message); } }
-
You are always returning the first row...
int count=0, b=0;
dr=ds.Tables[0].Rows[b];
comboBox1.Items.Add (dr[1].ToString());Regards, Gareth. (FKA gareth111)
Thanks for the reply Please guide me on what i should include to get all the data in that particular column.
-
Thanks for the reply Please guide me on what i should include to get all the data in that particular column.
Hi. You should databind:
Combobox1.DataSource = ds.Tables[0];
Combobox1.DisplayMember = "Column name for column to display";
Combobox1.ValueMember = "Column name for ID column";Kjetil
-
HI, Am trying to retreive some data into the Visual Studio C#.net (Winforms) combobox from a access database, but when i run it, i get only the first record, Please help me out. Thanks Vibin private void Form2_Load(object sender, System.EventArgs e) { int count=0, b=0; try { con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\JET PRINT LIMITED\\JET PRINT LIMITED.mdb"); da=new OleDbDataAdapter("select * from STOCKDET",con); con.Open(); ds=new DataSet(); da.Fill(ds,"STOCKDET"); comm.Connection=con; comm.CommandType=CommandType.Text; da=new OleDbDataAdapter(comm); dr=ds.Tables [0].Rows [b]; comboBox1.Items.Add (dr[1].ToString()); MessageBox.Show("data into Combobox successfuly!!!"); MessageBox.Show("Connected successfuly!!!"); } catch(Exception k) { MessageBox.Show(k.Message); } }
firstly you are using a reader with a dataset and that confuses more. if you want to retrieve what is in the command object and in future you won't manipulate the data, you should use only a datareader.so you have combined dataset and a datareader. your code above can be written like this
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\JET PRINT LIMITED\\JET PRINT LIMITED.mdb");
OleDbDataReader dr;
OleDbCommand comm = new SqlCommand();
comm.Connection=con;
comm.CommandType=CommandType.Text;
comm.CommandText = "select * from STOCKDET";con.Open();
dr=comm.ExecuteReader();
con.Close();
comboBox1.Datasource=dr;
MessageBox.Show("data into Combobox successfuly!!!");
MessageBox.Show("Connected successfuly!!!");
Hope it helps
Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za