asp.net - how to bind dataset with gridview -
this code .aspx.cs page
public string makequery() { string query = string.empty; if (ddlportal2.selectedvalue == "select" && tbfrom.text == null && tbto.text == null && ddlquery.selectedvalue == "select") { query = "select * form3 , order convert(varchar(25), [datetime], 101) between '" + tbfrom.text + "' , '" + tbto.text + "' desc"; return query; } else if(ddlportal2.selecteditem.text!="select" && tbfrom.text!=null && tbto.text!=null && ddlquery.selecteditem.text=="select") { query = "select * form3 portal='" + ddlportal2.selectedvalue + "' , order convert(varchar(25), [datetime], 101) between '" + tbfrom.text + "' , '" + tbto.text + "' desc"; return query; } else if (ddlportal2.selecteditem.text != "select" && tbfrom.text == null && tbto.text == null && ddlquery.selecteditem.text != "select") { query = "select * form3 portal='" + ddlportal2.selectedvalue + "' , order convert(varchar(25), [datetime], 101) between '" + tbfrom.text + "' , '" + tbto.text + "' desc"; return query; } return null; } public void gridviewbind() { string query = makequery(); dataset ds = new dataset(); if (ds != null) { if (ds.tables[0].rows.count != 0) { gridview1.datasource = ds; gridview1.databind(); } else { gridview1.datasource = null; gridview1.databind(); } } } protected void btnsearch_click(object sender, eventargs e) { this.gridviewbind(); }
} .aspx page code:
<div id="portal" runat="server"> <asp:label id="lblportal" runat="server" text="select portal"/> <asp:dropdownlist id="ddlportal2" runat="server" autopostback="true"> <asp:listitem>select</asp:listitem> <asp:listitem>travelong</asp:listitem> <asp:listitem>onetravel</asp:listitem> <asp:listitem>.uk-bsp</asp:listitem> <asp:listitem>.cv-yyz</asp:listitem> <asp:listitem>.cv-yvr</asp:listitem> </asp:dropdownlist> <asp:label id="lbfrom" text="from" runat="server" /> <asp:textbox id="tbfrom" runat="server" /> <asp:requiredfieldvalidator id="rfvfrom" runat="server" controltovalidate="tbfrom" errormessage="enter valid date">*</asp:requiredfieldvalidator> <asp:label id="lblto" text="to" runat="server" /> <asp:textbox id="tbto" runat="server" /> <asp:requiredfieldvalidator id="rfvto" runat="server" controltovalidate="tbto" errormessage="enter date">*</asp:requiredfieldvalidator> <asp:dropdownlist id="ddlquery" runat="server" width="87px"> <asp:listitem>all</asp:listitem> <asp:listitem value="query ">query</asp:listitem> <asp:listitem>non query</asp:listitem> </asp:dropdownlist> <asp:button id="btnsearch" runat="server" text="search" onclick="btnsearch_click" validationgroup="1"/><br /> <br /> <asp:validationsummary id="validationsummary1" runat="server" /> </div> <div id="main" runat="server"> <asp:gridview id="gridview1" runat="server" cellpadding="4" forecolor="#333333" gridlines="none" onrowcommand="gridview1_rowcommand" autogeneratecolumns="false"> <columns> <asp:templatefield headertext="select"> <itemtemplate> <asp:linkbutton commandname="cmdbind" runat="server" text='<%#eval("id")%>' id="id" tooltip='<%#eval("id")%>'>linkbutton </asp:linkbutton> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="date"> <itemtemplate> <asp:label id="lbldatetime" runat="server" text='<%# eval("datetime","{0:d}") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="portal" headertext="portal" sortexpression="portal" itemstyle-horizontalalign="center"/> <asp:boundfield datafield="tid" headertext="tid" sortexpression="tid" itemstyle-horizontalalign="center"/> <asp:boundfield datafield="pnr" headertext="pnr" sortexpression="pnr" itemstyle-horizontalalign="center"/> <asp:boundfield datafield="ticketnumber" headertext="ticketnumber" sortexpression="ticketnumber" itemstyle-horizontalalign="center"/> <asp:templatefield headertext="unused tkt amount" sortexpression="ddlunusedamount" itemstyle-horizontalalign="center"> <itemtemplate> <asp:label id="lblunusedamount" runat="server" text='<%#eval("unusedticketamount")+ "-" + eval("ddlunusedamount")%>' ></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="penality" sortexpression="ddlairlinepenality" itemstyle-horizontalalign="center"> <itemtemplate> <asp:label id="lblairlinepenality" runat="server" text='<%#eval("airlinepenality")+ "-" + eval("ddlairlinepenality")%>' ></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="net refund" sortexpression="ddlnetrefundprocess" itemstyle-horizontalalign="center"> <itemtemplate> <asp:label id="lblnetrefund" runat="server" text='<%#eval("netrefundprocess")+ "-" + eval("ddlnetrefundprocess")%>' ></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="refundable commission" sortexpression="ddlrefundablecommission" itemstyle-horizontalalign="center"> <itemtemplate> <asp:label id="lblrefundablecommission" runat="server" text='<%#eval("refundablecommission")+ "-" + eval("ddlrefundablecommission")%>' ></asp:label> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="esaccode" headertext="esaccode" sortexpression="esaccode" itemstyle-horizontalalign="center"/> <asp:boundfield datafield="waivercode" headertext="waivercode" sortexpression="waivercode" itemstyle-horizontalalign="center"/> <asp:boundfield datafield="refundtype" headertext="refundtype" sortexpression="refundtype" itemstyle-horizontalalign="center" /> <asp:boundfield datafield="comment" headertext="bart comment" itemstyle-horizontalalign="center"/> <asp:boundfield datafield="query" headertext="arc verified" itemstyle-horizontalalign="center" /> </columns>
now here make 2 function 1 make query , second bind query dataset
gridview
when bind dataset
cannot find table 0.
what want when select field , press search button can view details in grid according selected fields can't bind dataset
. can guys me this.
just make small changes in code below
public string makequery() { string query = string.empty; if (ddlportal2.selectedvalue == "select" && tbfrom.text == null && tbto.text == null && ddlquery.selectedvalue == "select") { query = "select * form3 , order convert(varchar(25), [datetime], 101) between '" + tbfrom.text + "' , '" + tbto.text + "' desc"; return query; } else if(ddlportal2.selecteditem.text!="select" && tbfrom.text!=null && tbto.text!=null && ddlquery.selecteditem.text=="select") { query = "select * form3 portal='" + ddlportal2.selectedvalue + "' , order convert(varchar(25), [datetime], 101) between '" + tbfrom.text + "' , '" + tbto.text + "' desc"; return query; } else if (ddlportal2.selecteditem.text != "select" && tbfrom.text == null && tbto.text == null && ddlquery.selecteditem.text != "select") { query = "select * form3 portal='" + ddlportal2.selectedvalue + "' , order convert(varchar(25), [datetime], 101) between '" + tbfrom.text + "' , '" + tbto.text + "' desc"; return query; } return null; } public void gridviewbind() { string query = makequery(); sqlconnection con=new sqlconnection("your connection string"); dataset ds = new dataset(); sqldataadapter ad= new sqldataadapter(query,con); ad.fill(ds); if (ds != null) { if (ds.tables[0].rows.count != 0) { gridview1.datasource = ds; gridview1.databind(); } else { gridview1.datasource = null; gridview1.databind(); } } } protected void btnsearch_click(object sender, eventargs e) { this.gridviewbind(); }
Comments
Post a Comment