c# - Dropdown List Search From SQL Database Using Repeater Control -


trying search database items displayed in dropdown list , when button clicked want output results repeater control. have working text boxes unsure how dropdown list.

sproc

create procedure dbo.spisearchbystring (     @housenearsearch varchar(50) = null,     @housenamesearch varchar(50) = null,     @housefamsearch varchar(50) = null,     @housetownsearch varchar(50) = null )     select         houses.name         ,houses.id         ,houses.townland         ,houses.near         ,houses.status         ,houses.built         ,houses.image     houses     (houses.near null or (houses.near '%' + @housenearsearch + '%' or houses.id '%' + @housenearsearch + '%'))  or   (houses.name null or  (houses.name '%' + @housenamesearch + '%' or houses.id '%' + @housenamesearch + '%'))  or   (houses.families null or  (houses.families '%' + @housefamsearch + '%' or houses.id '%' + @housefamsearch + '%'))  or   (houses.townland null or  (houses.townland '%' + @housetownsearch + '%' or houses.id '%' + @housetownsearch + '%'))     order houses.name     return 

aspx page

<div>                     <form id="name" style="margin-left: 30%" runat="server">                         name of house:                         <asp:textbox id="txtname" runat="server" style="margin-left:7%" ></asp:textbox>                     <br />                         <br />                         townland:                         <asp:dropdownlist id="lsttown" runat="server" width="174" style="margin-left:13.5%" ></asp:dropdownlist>                             <br />                         <br />                         near:                         <asp:textbox id="txtnear" runat="server" style="margin-left:19.6%"></asp:textbox>                          <br />                         <br />                         associated families:                         <asp:textbox id="txtfam" runat="server" style="margin-left:1.7%"></asp:textbox>                          <br />                         <br />                         <asp:button id="btnsearch" runat="server" text="search" onclick="btnsearch_click" cssclass="button"  style="margin-left:46%"/>                         </form>                         <br />                     <br />                         <h2><asp:label id="lblstatus" runat="server" text=""></asp:label></h2>                     </div>                  <ul id="rooms">                     <asp:repeater id="rptdatasearch" runat="server">                         <itemtemplate>                             <li>                                <a href='<%# "houseinfo.aspx?houseid=" + eval("id").tostring() %>'>                                     <img src='<%#databinder.eval(container.dataitem, "image")%>' alt="img" width="398" height="287"/>                                 <h2>                                    <a href='<%# "houseinfo.aspx?houseid=" + eval("id").tostring() %>'>                                         <asp:label runat="server" text='<%# eval("name") %>'></asp:label></a></h2>                                 <p>                                     <b>id: </b>                                     <%#databinder.eval(container.dataitem, "id")%>                                     <br />                                     <b>name of house: </b>                                      <%#databinder.eval(container.dataitem, "name")%>                                     <br />                                     <b>townland: </b>                                     <%#databinder.eval(container.dataitem, "townland")%>                                     <br />                                     <b>near: </b>                                     <%#databinder.eval(container.dataitem, "near")%>                                     <br />                                     <b>status/public access: </b>                                     <%#databinder.eval(container.dataitem, "status")%>                                     <br />                                      <b>date built: </b>                                     <%#databinder.eval(container.dataitem, "built")%>                                 </p>                             </li>                         </itemtemplate>                     </asp:repeater>         <asp:sqldatasource id="sqldatasource1" runat="server"          connectionstring="<%$ connectionstrings:connectionstring %>"          selectcommand="select * [houses]" providername="system.data.sqlclient"></asp:sqldatasource>                 </ul> 

code behind

protected void page_load(object sender, eventargs e)         {             string constr = configurationmanager.connectionstrings["connectionstring"].tostring();             sqlconnection con = new sqlconnection(constr);             con.open();              sqlcommand com = new sqlcommand("select distinct townland houses order townland asc", con);             sqldataadapter da = new sqldataadapter(com);             dataset ds = new dataset();             da.fill(ds);             lsttown.datatextfield = ds.tables[0].columns["townland"].tostring();             lsttown.datavaluefield = ds.tables[0].columns["townland"].tostring();             lsttown.datasource = ds.tables[0];             lsttown.databind();             lsttown.items.insert(0, new listitem(string.empty, string.empty));             lsttown.selectedindex = 0;         }          protected void btnsearch_click(object sender, eventargs e)         {             // name textbox search             if (txtname.text == "" | txtname.text == null)             {                 lblstatus.text = "search results";             }             else             {                 displaysearchresults(txtname.text);                 lblstatus.text = "";             }             txtname.visible = true;             // near textbox search             if (txtnear.text == "" | txtnear.text == null)             {                 lblstatus.text = "search results";             }             else             {                 displaysearchresults(txtnear.text);                 lblstatus.text = "";             }             txtnear.visible = true;             // family textbox search             if (txtfam.text == "" | txtfam.text == null)             {                 lblstatus.text = "search results";             }             else             {                 displaysearchresults(txtfam.text);                 lblstatus.text = "";             }             txtfam.visible = true;             // dropdown list search             if (lsttown.selectedvalue == "" | lsttown.selectedvalue == null)             {                 lblstatus.text = "search results";             }             else             {                 displaysearchresults(lsttown.selectedvalue);                 lblstatus.text = "";             }             lsttown.visible = true;         }          public void displaysearchresults(string strsearch)         {             //display search results in repeater             sqlcommand cmd = new sqlcommand("spisearchbystring", new sqlconnection(""));             cmd.commandtype = commandtype.storedprocedure;             cmd.parameters.addwithvalue("@housenamesearch", strsearch);             cmd.parameters.addwithvalue("@housenearsearch", strsearch);             cmd.parameters.addwithvalue("@housefamsearch", strsearch);             cmd.parameters.addwithvalue("@housetownsearch", strsearch);             cmd.connection.open();              rptdatasearch.datasource = cmd.executereader();             rptdatasearch.databind();         } 

textbox search

enter image description here

this displays results.

dropdown list search

enter image description here

this displays nothing.

you're problem binding , setting selectedindex = 0 on every single postback. if place breakpoint in page_load, notice every time click search button, breakpoint hits.

what need wrap logic in page_load ispostback condition. way not rebinding every single time , therefore keeping selectedindex @ index expect.

protected void page_load(object sender, eventargs e) {     if(!ispostback)     {         string constr = configurationmanager.connectionstrings["connectionstring"].tostring();         sqlconnection con = new sqlconnection(constr);         con.open();          sqlcommand com = new sqlcommand("select distinct townland houses order townland asc", con);         sqldataadapter da = new sqldataadapter(com);         dataset ds = new dataset();         da.fill(ds);         lsttown.datatextfield = ds.tables[0].columns["townland"].tostring();         lsttown.datavaluefield = ds.tables[0].columns["townland"].tostring();         lsttown.datasource = ds.tables[0];         lsttown.databind();         lsttown.items.insert(0, new listitem(string.empty, string.empty));         lsttown.selectedindex = 0;     } } 

Comments