c# - Want to populate a combobox with two related tables in access -
i have 2 related tables in access database, table1 , table2. in table1 have "cd_table1"(primarykey) , "cd_table2"(foreignkey). in table2 have "cd_table2"(primarykey) , "nm_atr"(string). want populate combobox these 2 tables, "displaymemberpath" needs "nm_atr" , "valuememberpath" needs "cd_table1".
my code:
oledbcommand cmd = new oledbcommand("select table1.cd_atributosnormais, table1.nm_atr, table2.cd_atributosnormais table1, table2 table1.cd_atributosnormais = table2.cd_atributosnormais", cn); oledbdatareader reader = cmd.executereader(); datatable table = new datatable(); table.load(reader); datarow row = table.newrow(); row["nm_atr"] = ""; table.rows.insertat(row, 0); this.btcmbatkbaatr1.datacontext = table.defaultview; this.btcmbatkbaatr1.displaymemberpath = "nm_atr"; this.btcmbatkbaatr1.selectedvaluepath = "cd_atributosnormais"; cmd.executenonquery(); reader.close(); reader.dispose(); } catch(exception ex) { messagebox.show(ex.message); } { cn.close(); cn.dispose(); }
where error , why combobox not display nothing? i'm opened connection , i'm using c# program , access 2010.
try if helps..
oledbcommand cmd = new oledbcommand("select table1.cd_atributosnormais cd_atributosnormais, table2.nm_atr nm_atr table1 left outer join table2 on table1.cd_atributosnormais = table2.cd_atributosnormais", cn); oledbdatareader reader = cmd.executereader(); datatable table = new datatable(); table.load(reader); datarow row = table.newrow(); row["nm_atr"] = ""; table.rows.insertat(row, 0); this.btcmbatkbaatr1.datacontext = table.defaultview; this.btcmbatkbaatr1.displaymemberpath = "nm_atr"; this.btcmbatkbaatr1.selectedvaluepath = "cd_atributosnormais"; cmd.executenonquery(); reader.close(); reader.dispose(); } catch(exception ex) { messagebox.show(ex.message); } { cn.close(); cn.dispose(); }
Comments
Post a Comment