• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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>
0
lenmor
Asked:
lenmor
  • 3
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
The first thing to do would be to output the SQL statement to make sure it is being formed correctly.
0
 
dunglaCommented:
string sqlText = "Select * from KL_Gronnesider where Bodytext LIKE '%"  + txtSok  + "%' and Kategori= " + DropDown.SelectedItem.Value;

should be

string sqlText = "Select * from KL_Gronnesider where Bodytext LIKE '%"  + txtSok.Text.Trim()  + "%' and Kategori= " + DropDown.SelectedItem.Value.ToString();
=============
If you + txtSok then your string will look like

Select * from KL_Gronnesider where Bodytext LIKE '%System.Web.UI.WebControls.TextBox%' and Kategori= " + DropDown.SelectedItem.Value.ToString()
0
 
lenmorAuthor Commented:
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??
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dunglaCommented:
How many columns in KL_Gronnesider table? can you list them here?
0
 
lenmorAuthor Commented:
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?
0
 
dunglaCommented:
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now