Click here to Skip to main content
15,885,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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;

}

} 

}

}
Posted
Updated 8-Jan-12 19:20pm
v2

While debugging,
1)check the searchParam that you are getting
C#
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}


2) just check if in the pageSource you are getting the desired result.
C#
//Set the datasource for DataList

lstSearch.DataSource = ds.Tables[0].DefaultView;

lstSearch.DataSource = pageSource; // check here

lstSearch.DataBind(); //gives exception here.


and tell the result
 
Share this answer
 
v2
Yes I have debugged it many times, and I do get the table in the dataset as well as PageSource.
and yeah SearchParam does contain the parameter entered on the page by user.
 
Share this answer
 
Comments
Mehdi Gholam 9-Jan-12 1:32am    
Please use the comments for further assistance, do not post solutions.
Hi,

Replace you code:
C#
if (Request.QueryString["query"] != null)
{
    string searchParam = Request.QueryString["query"].ToString();
    BindList(searchParam);
}

with:
C#
if (Request.QueryString["query"] != null)
{
    string Url ="search-result.aspx?";
    string searchParam = Request.QueryString["query"].ToString();
    BindList( Url + searchParam);
 
}
 
Share this answer
 
v2
Comments
RaisKazi 9-Jan-12 2:07am    
OP's comments -
Al Moje, I m passing qryString on buttonClick event. If I replaced these lines with yours, what would I pass on buttonClick?
Al Moje 9-Jan-12 2:20am    
If that so...
comments the line:
//string Url ="search-result.aspx?";

modify the declaration of Url variable from above as:
private static string Url ="search-result.aspx?";
Al Moje, I m passing qryString on buttonClick event. If I replaced these lines with yours, what would I pass on buttonClick?
 
Share this answer
 
Caught the culprit.
i added try-catch block to ItemDataBound and came to know tht the TxtSearch was containing null after the page was being redirected to itself. Hence I set the value of searchParam to TxtSearch and it worked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900