OledbException when trying to Insert values in two combobox
-
Hi, Here is below code as i am trying to populate two combobox for different values but getting OledbException while running: I have two tables called Packing and Products Packing=id,name Products:id,prodname,price,packing [defined by number] I have setup relation between them in MSAccess which is done.. Product.Packing=Number Packing.PackingName=Text Code:
private void Form1_Load(object sender, EventArgs e)
{string sql ="SELECT distinct ProductName FROM Products"; string sql2 = "SELECT pac.PackingName FROM Products prod,Packing pac where prod.Packing=pac.PackingName "; DataSet ds=new DataSet(); try { database = new OleDbConnection(connectionString); database.Open(); } catch (Exception x) { MessageBox.Show(x.Message); } OleDbDataAdapter adp=new OleDbDataAdapter(sql,database); adp = new OleDbDataAdapter(sql2,database); adp.Fill(ds,"ProductName"); adp.Fill(ds,"PackingName"); foreach (DataRow dr in ds.Tables\[0\].Rows) { comboBox3.Items.Add(dr\[0\].ToString()) ; comboBox1.Items.Add(dr\[1\].ToString()); } }
Any help please...Thanks,
-
Hi, Here is below code as i am trying to populate two combobox for different values but getting OledbException while running: I have two tables called Packing and Products Packing=id,name Products:id,prodname,price,packing [defined by number] I have setup relation between them in MSAccess which is done.. Product.Packing=Number Packing.PackingName=Text Code:
private void Form1_Load(object sender, EventArgs e)
{string sql ="SELECT distinct ProductName FROM Products"; string sql2 = "SELECT pac.PackingName FROM Products prod,Packing pac where prod.Packing=pac.PackingName "; DataSet ds=new DataSet(); try { database = new OleDbConnection(connectionString); database.Open(); } catch (Exception x) { MessageBox.Show(x.Message); } OleDbDataAdapter adp=new OleDbDataAdapter(sql,database); adp = new OleDbDataAdapter(sql2,database); adp.Fill(ds,"ProductName"); adp.Fill(ds,"PackingName"); foreach (DataRow dr in ds.Tables\[0\].Rows) { comboBox3.Items.Add(dr\[0\].ToString()) ; comboBox1.Items.Add(dr\[1\].ToString()); } }
Any help please...Thanks,
shah123 wrote:
OleDbDataAdapter adp=new OleDbDataAdapter(sql,database); adp = new OleDbDataAdapter(sql2,database);
you are using the same data adapter for box queries. Is this what you want to do? You need to process the first data adapter before reusing it or use different data adapter, even better, refactor your code and call it as many times as you want, for example
FillComboBox(comboBox, sql)
{
DataSet ds = new DataSet();
try
{
database = new OleDbConnection(connectionString);
database.Open();OleDbDataAdapter adp = new OleDbDataAdapter(sql, database); adp.Fill(ds, "tableName"); foreach (DataRow dr in ds.Tables\[0\].Rows) { comboBox.Items.Add(dr\[0\].ToString()); } } catch (Exception x) { MessageBox.Show(x.Message); } }
Yusuf
-
shah123 wrote:
OleDbDataAdapter adp=new OleDbDataAdapter(sql,database); adp = new OleDbDataAdapter(sql2,database);
you are using the same data adapter for box queries. Is this what you want to do? You need to process the first data adapter before reusing it or use different data adapter, even better, refactor your code and call it as many times as you want, for example
FillComboBox(comboBox, sql)
{
DataSet ds = new DataSet();
try
{
database = new OleDbConnection(connectionString);
database.Open();OleDbDataAdapter adp = new OleDbDataAdapter(sql, database); adp.Fill(ds, "tableName"); foreach (DataRow dr in ds.Tables\[0\].Rows) { comboBox.Items.Add(dr\[0\].ToString()); } } catch (Exception x) { MessageBox.Show(x.Message); } }
Yusuf
Thanks Yusuf, But i have one different issue then just oledb exception... I have two tables Products=ProdId,ProdName,Price,Packing(Number as datatype) Packing=packingId,PackingName (Text as datatype) Now below code is in C# ..first check pls if "sql2" is correct query and then when i execute an application i get an error "Expression Mismatch" on line: adp.Fill(ds,"PackingName"); Thanks, regards,
-
Thanks Yusuf, But i have one different issue then just oledb exception... I have two tables Products=ProdId,ProdName,Price,Packing(Number as datatype) Packing=packingId,PackingName (Text as datatype) Now below code is in C# ..first check pls if "sql2" is correct query and then when i execute an application i get an error "Expression Mismatch" on line: adp.Fill(ds,"PackingName"); Thanks, regards,
I got the solution of how to tackle and did it by doing SQL query again to be bit corrected..and now no exception...
private void Form1_Load(object sender, EventArgs e)
{string sql ="SELECT distinct ProductName FROM Products"; string sql2 = "SELECT DISTINCT Packing.PackingName FROM Packing INNER JOIN Products ON Packing.PackingID=Products.Packing"; DataSet ds=new DataSet(); try { database = new OleDbConnection(connectionString); database.Open(); } catch (Exception x) { MessageBox.Show(x.Message); } OleDbDataAdapter adp = new OleDbDataAdapter(sql, database); adp.Fill(ds, "ProductName"); adp = new OleDbDataAdapter(sql2, database); adp.Fill(ds,"PackingName"); foreach (DataRow dr in ds.Tables\[0\].Rows) { comboBox3.Items.Add(dr\[0\].ToString()) ; comboBox1.Items.Add(dr\[1\].ToString()); }
But now in combobox1 i get the same values as in combobox3? :confused: I think so pb in foreach loop? Any advise please...