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.Sql Client" %>
<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(ConnectionSt ring());
SqlCommand sqlCmd = new SqlCommand(sqlText,sqlConn );
sqlCmd.Connection.Open();
dr = sqlCmd.ExecuteReader(Syste m.Data.Com mandBehavi or.CloseCo nnection);
return dr;
}
private string ConnectionString()
{
return ConfigurationSettings.AppS ettings["C onnectionS tring"];
}
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.Valu e;
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:TextB ox>
</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>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql
<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(ConnectionSt
SqlCommand sqlCmd = new SqlCommand(sqlText,sqlConn
sqlCmd.Connection.Open();
dr = sqlCmd.ExecuteReader(Syste
return dr;
}
private string ConnectionString()
{
return ConfigurationSettings.AppS
}
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.Valu
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:TextB
</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.
</ItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.
</ItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.
</ItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.
</ItemTemplate>
</asp:DataList>
</table>
</table>
</body>
</html>
The first thing to do would be to output the SQL statement to make sure it is being formed correctly.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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??
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?
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.Valu e.ToString ();
It gives me all records even if I search blank or on an empty category.....
Something wrong with the sql?
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.Valu
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
something, dosomething, notso. And you define that Bodytext and Firmanavn -> this will return much more records