1
Reply

DataList.DataBind() error

ariez

ariez

Jan 9 2012 1:09 AM
1.7k
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;

}

}

Answers (1)