Click here to Skip to main content
15,041,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i display an excel cell value into a textbox but get some error in display an excel cell value into a combobox how to get this
Posted
Comments
BillWoodruff 19-Nov-13 2:00am
   
Please show us your code, and describe the error in detail.
dhaval082 19-Nov-13 2:14am
   
private void btnshow_Click(object sender, EventArgs e)
{
string connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\KBE\\Rod Piston_R1.xls';Extended Properties=Excel 8.0;";
OleDbConnection MyConnection;
DataSet ds;
OleDbDataAdapter MyCommand;
MyConnection = new OleDbConnection(connStr);
MyCommand = new OleDbDataAdapter("select * from [INPUT$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
comboBox1.Text = dt.Rows[1]["Value"].ToString();
comboBox2.Text = dt.Rows[2]["Value"].ToString();
textBox1.Text = dt.Rows[3]["Value"].ToString();
textBox2.Text = dt.Rows[4]["Value"].ToString();
textBox3.Text = dt.Rows[5]["Value"].ToString();
textBox4.Text = dt.Rows[6]["Value"].ToString();
textBox5.Text = dt.Rows[7]["Value"].ToString();
textBox6.Text = dt.Rows[8]["Value"].ToString();
textBox7.Text = dt.Rows[9]["Value"].ToString();
textBox8.Text = dt.Rows[10]["Value"].ToString();
comboBox3.Text = dt.Rows[11]["Value"].ToString();
MyConnection.Close();
}
Er Daljeet Singh 19-Nov-13 2:04am
   
post your code where you are getting error.
dhaval082 19-Nov-13 2:15am
   
private void btnshow_Click(object sender, EventArgs e)
{
string connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\KBE\\Rod Piston_R1.xls';Extended Properties=Excel 8.0;";
OleDbConnection MyConnection;
DataSet ds;
OleDbDataAdapter MyCommand;
MyConnection = new OleDbConnection(connStr);
MyCommand = new OleDbDataAdapter("select * from [INPUT$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
comboBox1.Text = dt.Rows[1]["Value"].ToString();
comboBox2.Text = dt.Rows[2]["Value"].ToString();
textBox1.Text = dt.Rows[3]["Value"].ToString();
textBox2.Text = dt.Rows[4]["Value"].ToString();
textBox3.Text = dt.Rows[5]["Value"].ToString();
textBox4.Text = dt.Rows[6]["Value"].ToString();
textBox5.Text = dt.Rows[7]["Value"].ToString();
textBox6.Text = dt.Rows[8]["Value"].ToString();
textBox7.Text = dt.Rows[9]["Value"].ToString();
textBox8.Text = dt.Rows[10]["Value"].ToString();
comboBox3.Text = dt.Rows[11]["Value"].ToString();
MyConnection.Close();
}
Er Daljeet Singh 19-Nov-13 2:21am
   
first check whether you datatable in having value or not by putting breakpoint.
if yes then what error you are getting..

1 solution

You can't Directly Bind a Text to ComboBox if the Item is not in Combobox so
Use
comboBox1.Items.Add(dt.Rows[1]["Value"].ToString());
It will Add the Value to Combobox Then You Select it By
comboBox1.Text=dt.Rows[1]["Value"].ToString(); If More then One Items Available or combox1.SelectedIndex=0; If only one Item is Available
or
comboBox1.Items.Insert(0,dt.Rows[1]["Value"].ToString());
Here First Parameter is Position
By Using this You can Insert a New Item at a Defined Position and Then You can Select It By
comboBox1.SelectedIndex=position; Here it will be 0;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900