字符串长度不能为零。参数名称:oldV [英] String cannot be of zero length. Parameter name: oldV
问题描述
大家好,
i在我的应用程序中创建搜索选项时出现了一些错误,在这是使用c#处理asp.net应用程序。我输入的是我的文本框中的一些文本和我单击搜索按钮它工作正常,但没有在文本框中输入任何内容并单击搜索按钮,其显示以下错误,
字符串不能长度为零。
参数名称:oldV
点击事件代码,
受保护 void imgSearch_Click( object sender,ImageClickEventArgs e)
{
Session.Add( searchterm ,txtSearch.Text.Trim());
Response.Redirect( cheers_search-result.aspx);
}
请参阅我的以下源代码并告诉我是否有任何解决方案
clsOleCon objOle = new clsOleCon();
System.Text.StringBuilder strtodisplay = new System.Text.StringBuilder();
受保护 void Page_Load( object sender,EventArgs e)
{
string searchterm = Session [ < span class =code-string> searchterm]。ToString();
string query = SELECT * from content其中的内容如'% + 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 resul = ;
if (dr.ItemArray [ 1 ]。ToString()。Length > 150 )
{
results = dr.ItemArray [ 1 ]。ToString()。删除( 150 ,(dr.ItemArray [ 1 ]。ToString()。长度 - 150 ));
results = resul.ToLower();
得到= outcome.Replace(searchterm, < Span class = searchtext_cheers > + searchterm + < /跨度>中跨度>);
// results = resul.Replace(searchterm,< span class =searchtext> + searchterm +< / span>);
}
else
{
result = dr.ItemArray [ 1 ]。ToString();
得到= outcome.Replace(searchterm, < Span class = searchtext_cheers > + searchterm + < /跨度>中跨度>);
}
结果=结果+ ....;
strtodisplay.Append( < tr>< td width = 95%> 跨度>);
strtodisplay.Append( < a href = + dr.ItemArray [ 2 ]。ToString()+ >);
strtodisplay.Append( < span class = footer > +结果+ < /跨度> 中跨度>);
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( 找不到匹配项);
strtodisplay.Append( < / td>< / tr>< / table>< /跨度>);
lblSearchresult.Text = strtodisplay.ToString();
}
}
提前谢谢
问题在于行Session.Add(searchterm,txtSearch.Text.Trim());
你需要这样做,因为会话值不能成为string.Empty。
if (!string.IsNullOrEmpty(txtSearch.Text。修剪()))
{
Session.Add( searchterm, txtSearch.Text.Trim());
}
其他
{
// < span class =code-comment>如果没有,可以从会话中删除搜索词,或者设置默认值。
}
您确定何时将查询文本解析为SELECT * 来自内容其中内容喜欢 ' %%'会获取任何结果吗?您应该尝试为空字符串添加客户端验证,或者在查询中添加一个条件,如果字符串为空,则返回所有结果。
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%> </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
The problem will lie with the lineSession.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. }
Are you sure when the query text is parsed toSELECT * 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.
这篇关于字符串长度不能为零。参数名称:oldV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!