Hello,
Any help will be much appreciated.
I have created a page tht will show search results on a datalist. this page was running perfectly before the addition of following lines in the page Load and BtnSearch_Click respectively.
if (Request.QueryString["query"] != null)
{
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}
....................................
string qryString = Url; //Request.Url.ToString();
qryString += "?query=" + TxtSearch.Text.Trim();
Response.Redirect(qryString);
}
................................................
Now the problem is at lstSearch.DataBind();. It gives exception “String Value can not be null. Parameter name:Old value”. I googled the error but most of the ppl who encountered this problem were those who upgraded to newer versions of VS and thts not the case with me..
then I replaced the DataList with Repeater. It binds the data perfectly but it always picks zero index (shown in the following lines) , due to which I again the same exception at text replacement at the ItemDataBound event.
int index = _strContent.IndexOf(TxtSearch.Text, StringComparison.OrdinalIgnoreCase);
//Find substring with the help of that Index
string sub_strContent = _strContent.Substring(index, TxtSearch.Text.Length);
//Highlight the substring
string _strHighlightText = "<span class=\"highlight\">" + sub_strContent + "</span>";
//Replace the text with highlighted text
_strContent = _strContent.Replace(sub_strContent, _strHighlightText);
...............................................
Below is the complete code.Note that I have also added pagin to my .aspx page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace abc
{
public partial class search_result : System.Web.UI.Page
{
PagedDataSource pageSource;
//'currentpage' is used to keep track of the pages binded to the DataList
public static int currentpage = 0;
string Url ="search-result.aspx";
protected void Page_Load(object sender, EventArgs e)
{
lbtnNext1.Visible = false;
lbtnPrev1.Visible = false;
if (!Page.IsPostBack)
{
lblResults1.Visible = false;
lblResults2.Visible = false;
lblResults3.Visible = false;
if (Request.QueryString["query"] != null)
{
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}
}
}
protected void BtnSearch_Click(object sender, EventArgs e)
{
string qryString = Url; //Request.Url.ToString();
qryString += "?query=" + TxtSearch.Text.Trim();
Response.Redirect(qryString);
//if (TxtSearch.Text != null && TxtSearch.Text != string.Empty)
//{ BindList(); }
//else { }
}
protected void Item_Click(object sender, DataListCommandEventArgs e)
{
Response.Redirect(e.CommandName);
}
protected void lbtnPrev_Click(object sender, EventArgs e)
{
// currentpage -= 1;
//BindList();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
//currentpage += 1;
//BindList();
}
public void BindList(string search)
{
try
{
BLL.Admin adminobj = new BLL.Admin(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
DataSet ds = adminobj.Search(search);
int count = ds.Tables[0].Rows.Count;
//Bind data to the DataList if matching results are found
if (count != 0)
{
lblResults1.Visible = true;
lblResults2.Visible = true;
lblResults3.Visible = false;
if (count == 1)
{
lblResults1.Text = count + " Result for ";
lblResults2.Text = TxtSearch.Text;
}
else
{
lblResults1.Text = count + " Results for ";
lblResults2.Text = TxtSearch.Text;
}
pageSource = new PagedDataSource();
pageSource.AllowPaging = true;
pageSource.DataSource = ds.Tables[0].DefaultView;
// Set the number of results to be displayed below.
//e.g if you want to display 10 results per page, replace 5 by 10.
int a = 5;
pageSource.PageSize = a;
pageSource.CurrentPageIndex = currentpage;
// Disable the linkbuttons if it is first or last page.
lbtnNext1.Enabled = !pageSource.IsLastPage;
lbtnPrev1.Enabled = !pageSource.IsFirstPage;
//Set the datasource for DataList
lstSearch.DataSource = ds.Tables[0].DefaultView;
lstSearch.DataSource = pageSource;
lstSearch.DataBind(); //gives exception here.
// Keep the LinkButtons invisible if the returned rows are less than or equal to 5
if (count > a)
{
lbtnNext1.Visible = true;
lbtnPrev1.Visible = true;
}
}
else
{
//Display a message if the search doesnot match any results
lblResults1.Visible = true;
lblResults2.Visible = true;
lblResults3.Visible = true;
lblResults1.Text = "No Results found for ";
lblResults2.Text = TxtSearch.Text;
lblResults3.Text= ". Make sure all words are spelled correctly.";
lstSearch.DataSource = pageSource;
lstSearch.DataBind();
}
}
catch (Exception ex)
{ Console.Write(ex.Message); }
}
protected void lstSearch_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblContent = (Label)e.Item.FindControl("lblContent");
string _strContent = lblContent.Text;
//Get the index of entered word(s)
int index = _strContent.IndexOf(TxtSearch.Text, StringComparison.OrdinalIgnoreCase);
//Find substring with the help of that Index
string sub_strContent = _strContent.Substring(index, TxtSearch.Text.Length);
//Highlight the substring
string _strHighlightText = "<span class=\"highlight\">" + sub_strContent + "</span>";
//Replace the text with highlighted text
_strContent = _strContent.Replace(sub_strContent, _strHighlightText);
lblContent.Text = _strContent;
}
}
protected void lstSearch_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
........................................................................................................................
<div id="searchBox">
<asp:TextBox ID="TxtSearch" runat="server" Width="80%"></asp:TextBox>
<asp:Button ID="BtnSearch" runat="server" Text="Search" OnClick="BtnSearch_Click" />
</div>
<div id="DispalyLabel">
<br />
<asp:Label ID="lblResults1" runat="server" Text=" "></asp:Label><asp:Label ID="lblResults2"
runat="server" Text="Label" Font-Bold="true"></asp:Label><asp:Label ID="lblResults3" runat="server"
Text=" "></asp:Label>
</div>
<br />
<br />
<asp:DataList ID="lstSearch" runat="server" OnItemCommand="Item_Click" HorizontalAlign="Justify"
ShowHeader="true" Width="100%" CssClass="searchResult"
onitemdatabound="lstSearch_ItemDataBound"
onselectedindexchanged="lstSearch_SelectedIndexChanged">
<AlternatingItemStyle BackColor="White" ForeColor="#333333" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<ItemTemplate>
<table width="100%" cellpadding="5" cellspacing="0" border="0">
<tr>
<td>
<h4>
<asp:HyperLink ID="HyperLink1" Target="_blank" NavigateUrl='<%# "~/" + DataBinder.Eval(Container.DataItem,"PageName")%>'
Text='<%# DataBinder.Eval(Container.DataItem,"Page_Title")%>' runat="server"></asp:HyperLink></h4>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblContent" runat="server" Font-Bold="false" Text='<%# (DataBinder.Eval(Container.DataItem,"HTML_Content"))%>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:HyperLink ID="HyperLink2" Target="_blank" NavigateUrl='<%# "~/" + DataBinder.Eval(Container.DataItem,"PageName")%>'
Text='<%# DataBinder.Eval(Container.DataItem,"PageName")%>' runat="server"></asp:HyperLink>
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
<div id="LinkButton">
<table>
<tr><td> </td></tr>
<tr>
<td align="left">
<asp:LinkButton ID="lbtnPrev1" runat="server" OnClick="lbtnPrev_Click" ForeColor="Black">Prev</asp:LinkButton>
</td>
<td align="right">
<asp:LinkButton ID="lbtnNext1" runat="server" OnClick="lbtnNext_Click" ForeColor="Black">Next</asp:LinkButton>
</td>
</tr>
</table>
</div>