c# - Select value from database based on dropdownlist value -
i have database table leave_rec(name,date1,leave,ltype)
, dropdown list
, gridview
.
i want such that,when select month(e.g. february) in dropdown list gridview should display table values february only(e.g.rohan leuva,2/28/2013,full,casual)
,means record has month=2 (february).
how overcome issue? tried can display values in gridview
@ moment. appriciated.
sqlconnection conn = new sqlconnection(); conn.connectionstring=system.configuration.configurationmanager.connectionstrings["leave"].connectionstring; conn.open(); sqlcommand cmd = new sqlcommand("select date1,leave,ltype leave_rec name='" + dropdownlist1.selectedvalue + "'", conn); sqldataadapter da = new sqldataadapter(cmd); dataset ds = new dataset(); da.fill(ds); gridview1.datasource = ds; gridview1.databind();
the above code displays date1
,leave
,ltype
dropdownlist1.selectedvalue.
want have second dropdown in months there. when select february in second one, grid should display value dropdownlist1.selectedvalue february only.
first, query needs this:
select date1, leave, ltype leave_rec month(date1) = 2 // february
then, integrating code:
sqlcommand cmd = new sqlcommand("select date1, leave, ltype leave_rec month(date1) = @p1", conn); cmd.parameters.add(new sqlparameter("p1", combo.selectedkey));
use parameters instead of string concatenation avoid sql injection, see example here: http://www.dotnetperls.com/sqlparameter
(use own control names "combo.selectedkey", of course)
Comments
Post a Comment