Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DataBinding
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;
 
}
 
} 
 
}
 
}
Posted 8-Jan-12 19:07pm
ariez88560
Edited 8-Jan-12 19:20pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

While debugging,
1)check the searchParam that you are getting
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}
 
2) just check if in the pageSource you are getting the desired result.
//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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Mehdi Gholam at 9-Jan-12 1:32am
   
Please use the comments for further assistance, do not post solutions.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Replace you code:
if (Request.QueryString["query"] != null)
{
    string searchParam = Request.QueryString["query"].ToString();
    BindList(searchParam);
}
with:
if (Request.QueryString["query"] != null)
{
    string Url ="search-result.aspx?";
    string searchParam = Request.QueryString["query"].ToString();
    BindList( Url + searchParam);
 
}
  Permalink  
v2
Comments
RaisKazi at 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 at 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?";
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Al Moje, I m passing qryString on buttonClick event. If I replaced these lines with yours, what would I pass on buttonClick?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 CPallini 450
2 OriginalGriff 270
3 George Jonsson 256
4 Suvabrata Roy 229
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 9 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100