Tuesday, 15 January 2013

c# - Binding dropdownlist to dataset -



c# - Binding dropdownlist to dataset -

i'm filling datatable stored procedure , filling dataset datatable i'm getting "object reference not set instance of object" @ line ddl.datasource = ds.

how can solve binding?

using (var sctyche = new sqlconnection(configurationmanager.connectionstrings["kondorconnectionconnectionstring"].connectionstring)) { var ddl = (dropdownlist)gridview1.findcontrol("dropdownlistfolders1"); var da = new sqldataadapter(); var dt = new datatable(); var ds = new dataset(); sctyche.open(); var cmdtyche = sctyche.createcommand(); cmdtyche.commandtype = commandtype.storedprocedure; cmdtyche.commandtext = "dbo.spgetfolders"; cmdtyche.commandtimeout = 60; cmdtyche.parameters.add("@nbranchid", sqldbtype.int).value = intbranchid; da.selectcommand = cmdtyche; da.fill(dt); ds.tables.add(dt); ddl.datasource = ds; ddl.datatextfield = "strshort"; ddl.datavaluefield = "nid"; ddl.databind(); cmdtyche.parameters.clear(); sctyche.dispose(); sctyche.close(); }

i fill dataset adapter. ds contains table collection. depending on how many result sets stored proc returns, reflect in collection tables[0], tables[1] etc... in case, assuming it's returning 1. approach, don't need added datatable variable.

also, dispose , close kind of redundant since using statement designed handle via idisposable interface.

// illustration da.fill(ds); if (ds.tables.count > 0) { ddl.datasource = ds.tables[0]; // etc.... }

c# asp.net code-behind sqlconnection

No comments:

Post a Comment