asp.net - How do I populate an ASPX C# Form with multiple dropdowns from different related tables? -
this first attempt @ building website company. have 1 database 3 tables. structured below.
tblsitedataentryform
id
sitename
sitetype
sitesubtype
tblprimarytypes
id
primarytype
tblsubtypes
id
primarytypeid
subtype
an employee use simple form create new db entry in primary table. have form working. second form used update primary table. on update form have dropdown gets values using select query on primary database. dropdowns primary type , sub type filled based on row data primary table. primary type dropdown working sub type keeps throwing error. "ddlsubtype has selectedvalue invalid because not exist in list of items. parameter name: value"
here code. appreciated.
using system; using system.collections.generic; using system.configuration; using system.data; using system.data.sqlclient; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; public partial class ddef_update : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!page.ispostback) { loadinfo(); } } protected void loadinfo() { sqlconnection connection; sqlcommand command; sqldataadapter adapter = new sqldataadapter(); dataset ds = new dataset(); int = 0; string firstsql = null; string secondsql = null; string thirdsql = null; datatable sites = new datatable(); datatable types = new datatable(); datatable subtypes = new datatable(); firstsql = "select id, sitename tblsitedataentryform"; secondsql = "select id, primarytype tblprimarytypes"; thirdsql = "select id, subtype tblsubtypes"; connection = new sqlconnection(configurationmanager.connectionstrings["myconsstring"].tostring()); { connection.open(); command = new sqlcommand(firstsql, connection); adapter.selectcommand = command; adapter.fill(sites); ddlsitename.datasource = sites; ddlsitename.datatextfield = "sitename"; ddlsitename.datavaluefield = "id"; ddlsitename.databind(); adapter.selectcommand.commandtext = secondsql; adapter.fill(types); ddlprimarytype.datasource = types; ddlprimarytype.datatextfield = "primarytype"; ddlprimarytype.datavaluefield = "id"; ddlprimarytype.databind(); adapter.selectcommand.commandtext = thirdsql; adapter.fill(subtypes); ddlsubtype.datasource = subtypes; ddlsubtype.datatextfield = "subtype"; ddlsubtype.datavaluefield = "id"; ddlsubtype.databind(); adapter.dispose(); command.dispose(); connection.close(); } } protected void ddlsitename_selectedindexchanged(object sender, eventargs e) { string selected = ddlsitename.selecteditem.value; sqlconnection connection = new sqlconnection(configurationmanager.connectionstrings["myconsstring"].tostring()); using (connection) { sqlcommand command = new sqlcommand("select * tblsitedataentryform id= @id", connection); command.parameters.addwithvalue("@id", selected); command.commandtype = commandtype.text; connection.open(); sqldatareader reader = command.executereader(); using (reader) { if (reader.hasrows) { reader.read(); ddlprimarytype.text = reader["sitetype"].tostring(); ddlsubtype.text = reader["sitesubtype"].tostring(); } } } } }
i able solve problem. sitesubtype in primary table holding text value. changed integer , works
Comments
Post a Comment