Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i got some errors while am creating search option in my application , in this am handling asp.net application using c#.The thing is i enter some text in my textbox and i click search button its working fine, but without entering anything in the textbox and click search button ,its showing following error,

"String cannot be of zero length.
Parameter name: oldV "

click event code,
C#
protected void imgSearch_Click(object sender, ImageClickEventArgs e)
{
   Session.Add("searchterm", txtSearch.Text.Trim());
   Response.Redirect("cheers_search-result.aspx");
}

please see my below source code and let me know if any solution
C#
clsOleCon objOle = new clsOleCon();
System.Text.StringBuilder strtodisplay = new System.Text.StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
   string searchterm = Session["searchterm"].ToString();
   string query = "SELECT * from content where contents like '%" + searchterm + "%';";
   DataTable dt = objOle.selectquery(query);

   if (dt.Rows.Count > 0)
   {
      strtodisplay.Append("<table width=95% border=0 align=center cellpadding=0 cellspacing=0 class="top-8">");
      foreach (DataRow dr in dt.Rows)
      {
         string resultant = "";
         if (dr.ItemArray[1].ToString().Length > 150)
         {
            resultant = dr.ItemArray[1].ToString().Remove(150, (dr.ItemArray[1].ToString().Length - 150));

            resultant = resultant.ToLower();
            resultant = resultant.Replace(searchterm, "<Span class="searchtext_cheers">" + searchterm + "</span>");
            // resultant = resultant.Replace(searchterm, "<Span class="searchtext">" + searchterm + "</span>");

         }
         else
         {
            resultant = dr.ItemArray[1].ToString();
            resultant = resultant.Replace(searchterm, "<Span class="searchtext_cheers">" + searchterm + "</span>");
         }
         resultant = resultant + " ....";

         strtodisplay.Append("<tr><td width=95%>");
         strtodisplay.Append("<a href=" + dr.ItemArray[2].ToString() + ">");
         strtodisplay.Append("<span class="footer">" + resultant + "</span>");
         strtodisplay.Append("</a>");
         strtodisplay.Append("</td></tr>");
         strtodisplay.Append("<tr><td width=95% class="searchlink_cheers"><a  class="searchlink_cheers" href=" + dr.ItemArray[2].ToString() + ">" + dr.ItemArray[2].ToString() + "</a></td></tr>");
         strtodisplay.Append("<tr><td width=95%>&nbsp;</td></tr>");
      }
      strtodisplay.Append("</table>");
      lblSearchresult.Text = strtodisplay.ToString();
   }
   else
   {
      strtodisplay.Append("<table width=100% border=0 align=center cellpadding=0 cellspacing=0 class="top-8"><tr><td width=100%  class="searchresult_nomatch">");
      strtodisplay.Append("No Matches found");
      strtodisplay.Append("</td></tr></table>");
      lblSearchresult.Text = strtodisplay.ToString();
   }
}

thanks in advance
Posted
Updated 4-Jan-13 4:10am
v2

The problem will lie with the line Session.Add("searchterm", txtSearch.Text.Trim());
You need to do something like this because Session values can't be string.Empty.
C#
if( !string.IsNullOrEmpty(txtSearch.Text.Trim()) )
{
   Session.Add("searchterm", txtSearch.Text.Trim());
}
else
{
   // You could either remove search term from the Session if there isn't one, or set a default value.
}
 
Share this answer
 
Comments
stellus 5-Jan-13 7:02am    
thank u very much the code which u provide is workin fine,
Jibesh 6-Jan-13 14:31pm    
If this solution works you can mark this solution as resolved.
Jibesh 6-Jan-13 14:30pm    
my 5+
fjdiewornncalwe 7-Jan-13 9:08am    
Thanks, jibesh.
Are you sure when the query text is parsed to
SQL
SELECT * from content where contents like '%%'
will fetch you any result? You should try adding client side validation for blank string or add a condition in your query that if the string is blank return all result.
 
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