Link to home
Start Free TrialLog in
Avatar of lenmor
lenmor

asked on

What's wrong with this search, it doesn't return a value

<%@ Page Language="c#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">

public void Page_Load(object sender, System.EventArgs e)
{
  if (!IsPostBack)
                  {
        string sqlText = "select Id, Navn from KL_Gronnesiderkat order by Navn";
        DropDown.DataSource = GetDr( sqlText );
        DropDown.DataBind();
      }

}


 private SqlDataReader GetDr( string sqlText)
 {
 SqlDataReader dr;
 SqlConnection sqlConn = new SqlConnection(ConnectionString());
 SqlCommand sqlCmd = new SqlCommand(sqlText,sqlConn);
 sqlCmd.Connection.Open();
 dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
 return dr;
 }

 private string ConnectionString()
 {
 return ConfigurationSettings.AppSettings["ConnectionString"];
 }

 void btnSok_Click(Object Source, EventArgs E)
 {
     if (DropDown.SelectedIndex != -1)
     {
      string sqlText = "Select * from KL_Gronnesider where Bodytext LIKE '%"  + txtSok  + "%' and Kategori= " + DropDown.SelectedItem.Value;
      dlAnnonse.DataSource = GetDr( sqlText );
      dlAnnonse.DataBind();
      }

 }


</script>
<html>
<title>Grønne Sider</title>
</head>
<body>
<form id="form1" method="get"  runat="server">
<table>
       <tr>
           <td>
               Søk: <asp:TextBox id="txtSok" Width="250px" runat="server" Visible="True"></asp:TextBox>
           </td>
             <td>
              <asp:Dropdownlist id="DropDown" DataValueField = "Id" DataTextField = "Navn" autoPostBack="true" runat="server" />
             </td>
             <td>
              <asp:Button id="btnSok" Text="Søk" OnClick="btnSok_Click" toolTip="Klikk her for å søke i annonser" runat="server" />
             </td>
       </tr>
       </form>
        <table>
           <tr>
               <asp:DataList id="dlAnnonse" runat="server" RepeatLayout="Table">


               <ItemTemplate>
               <%# DataBinder.Eval(Container.DataItem, "Firmanavn")%>
                </ItemTemplate>

               <ItemTemplate>
               <%# DataBinder.Eval(Container.DataItem, "Adresse")%>
                </ItemTemplate>

                    <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "Telefon")%>
                     </ItemTemplate>

               <ItemTemplate>
               <%# DataBinder.Eval(Container.DataItem, "Bodytekst")%>
                </ItemTemplate>
            </asp:DataList>



       </table>


</table>
</body>

</html>
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

The first thing to do would be to output the SQL statement to make sure it is being formed correctly.
ASKER CERTIFIED SOLUTION
Avatar of dungla
dungla
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lenmor
lenmor

ASKER

http://www.intellicom.no/kulturlandskap/gronnesider.aspx

ok, that helped, but why is the datalist only giving me the bodytext, it should display more from that record??
How many columns in KL_Gronnesider table? can you list them here?
Avatar of lenmor

ASKER

I solved that, but a new problem occured
You will get your poits dugla:)

When I put:
 string sqlText = "Select * from KL_Gronnesider where Bodytext LIKE '%"  + txtSok.Text.Trim()  +  "%' OR Firmanavn LIKE '%"  + txtSok.Text.Trim()  + "%'  and Kategori= " + DropDown.SelectedItem.Value.ToString();

It gives me all records even if I search blank or on an empty category.....

Something wrong with the sql?
It gives me all records >> all records that have txtSok.Text.Trim() inside it. If you look for text "so" in this will match:
something, dosomething, notso. And you define that Bodytext and Firmanavn -> this will return much more records