asp.net - databind in gridview from dropdownlists -
i have 2 dropdownlists on webform. dlname , dlstage. have gridview needs based on values selected these 2 drop downs. currently, not filtering gridview , using following code:
private void binddata() { string query = "select [annotationnumber],[annotationby],[annotationtype],[businessunit] unit,[errortype],[actualagencyerror],annotationcomments,[sgkcomments],[actualagencyerror],cust,name,annotationdate vw_gridviewsource "; sqlcommand cmd = new sqlcommand(query); gvsummary.datasource = getdata(cmd); gvsummary.databind(); }
what have gridview check drop downs , bind data based on values in drop down lists.
at end of select statement 2 values name , annotationdate variables need matched. included them in query because trying use datakey bind data failed.
i pretty new can use possible.
try this:
// both drop down lists need selected value or don't query , bind if(!string.isnullorempty(dlname.selectedvalue) && !string.isnullorempty(dlstage.selectedvalue) { string selectedname = dlname.selectedvalue; string selectedannotationdate = dlstage.selectedvalue; // use parameterized sql instead of in-line sql avoid sql injection risks sqlcommand cmd = new sqlcommand(); cmd.connection = yourconnectionobject; cmd.commandtext = "select [annotationnumber],[annotationby],[annotationtype], [businessunit] unit,[errortype],[actualagencyerror],annotationcomments, [sgkcomments],[actualagencyerror],cust,name,annotationdate vw_gridviewsource name = @p1 , annotationdate = @p2"; cmd.parameters.add("@p1", sqldbtype.varchar, 100, selectedname); cmd.parameters.add("@p2", sqldbtype.varchar, 100, selectedannotationdate); gvsummary.datasource = getdata(cmd); gvsummary.databind(); }
note: substitute correct database types , sizes @p1
, @p2
above.
Comments
Post a Comment