Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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,
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
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 4-Jan-13 4:05am
Edited 4-Jan-13 4:10am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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.
}
  Permalink  
Comments
stellus at 5-Jan-13 7:02am
   
thank u very much the code which u provide is workin fine,
jibesh at 6-Jan-13 14:31pm
   
If this solution works you can mark this solution as resolved.
jibesh at 6-Jan-13 14:30pm
   
my 5+
Marcus Kramer at 7-Jan-13 9:08am
   
Thanks, jibesh.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Are you sure when the query text is parsed to
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 4 Jan 2013
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