大家早上好,我如何将此过滤器表达式转换为参数化查询但仍显示在网格视图中?现在,如果我搜索 %^&%* 它会崩溃。
C#:
public static bool IsDate(Object obj)
{
string strDate = obj.ToString();
try
{
DateTime dt = DateTime.Parse(strDate);
if (dt != DateTime.MinValue && dt != DateTime.MaxValue)
return true;
return false;
}
catch
{
return false;
}
}
protected void BtnWinnersSearch_Click(object sender, EventArgs e)
{
string searchText = txtWinnersSearch.Text.Replace("'", "''").Trim();
bool isDate = IsDate(searchText);
GridViewWinners.Visible = true;
if (isDate == true)
{
SqlDataSource4.FilterExpression = "dob" + " ='" + Convert.ToDateTime(searchText).ToString("yyyy-MM-dd") + "'";
}
else
{
SqlDataSource4.FilterExpression = "nickname like '%" + searchText + "%' or username like '%" + searchText +
"%' or clubnumber like '%" + searchText + "%' or firstname like '%" +
searchText + "%' or lastname like '%" + searchText +
"%' or email like '%" + searchText + "%'";
}
}
ASP:
<asp:Panel ID="pnlWinners" DefaultButton="BtnWinnersSearch" runat="server" Visible="False">
<table>
<tr>
<td align="center"><b>MANAGE WINNERS</b> -
<asp:Button ID="BtnWinnerAdd" runat="server" Text="Add"
onclick="BtnWinnerAdd_Click" /> |
<asp:TextBox ID="txtWinnersSearch" runat="server" MaxLength="220"></asp:TextBox>
<asp:Button ID="BtnWinnersSearch"
runat="server" Text="Search" onclick="BtnWinnersSearch_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridViewWinners" runat="server" AutoGenerateColumns="False" DataKeyNames="PlayerID"
DataSourceID="SqlDataSource4"
EmptyDataText="There are no winners to display." CellPadding="4"
AllowSorting="True"
onsorting="GridViewWinners_Sorting"
ForeColor="#333333"
Visible="False"
onselectedindexchanged="GridViewWinners_SelectedIndexChanged"
FooterStyle-Wrap="False" HeaderStyle-Wrap="False" PagerStyle-Wrap="False"
RowStyle-Wrap="False" SelectedRowStyle-Wrap="False" Font-Overline="False" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" HorizontalAlign="Center"
VerticalAlign="Middle" Wrap="False" />
<Columns>
<asp:CommandField ShowSelectButton="True"/>
<asp:BoundField DataField="PlayerID" HeaderText="PlayerID" InsertVisible="False"
ReadOnly="True" SortExpression="PlayerID" Visible="False" />
<asp:BoundField DataField="Nickname" HeaderText="Nickname"
SortExpression="Nickname" ItemStyle-Wrap="False">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name"
SortExpression="LastName" ItemStyle-Wrap="False">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="FirstName" HeaderText="First Name"
SortExpression="FirstName" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="ClubNumber" HeaderText="Account Number"
SortExpression="ClubNumber" />
<asp:BoundField DataField="Email" HeaderText="Email"
SortExpression="Email" />
<asp:BoundField DataField="DOB" HeaderText="D.O.B."
DataFormatString="{0:d}" SortExpression="DOB" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333"
HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</td>
</tr>
</table>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="****"
ProviderName="***"
SelectCommand="SELECT [PlayerID], [Nickname], [UserName], [ClubNumber], [FirstName], [Email], [LastName], [DOB] FROM [Players] order by lastname ASC">
</asp:SqlDataSource>
</asp:Content>
错误:
Like 运算符中的错误:字符串模式 '%@#%$%' 无效。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.EvaluateException:Like 运算符中的错误:字符串模式“%@#%$%”无效。
源错误:
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪:
[EvaluateException:Like 运算符中的错误:字符串模式 '%@#%$%' 无效。] System.Data.LikeNode.AnalyzePattern(String pat) +1251846 System.Data.LikeNode.Eval(DataRow 行,DataRowVersion 版本) +345 System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode right, DataRow row, DataRowVersion version, Int32[] recordNos) +13013
System.Data.BinaryNode.Eval(DataRow row, DataRowVersion version) +29
System. Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode right, DataRow row, DataRowVersion version, Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow row, DataRowVersion version) +29
System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode right, DataRow row, DataRowVersion version, Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow row, DataRowVersion version) +29
System.Data。 BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode right, DataRow row, DataRowVersion version, Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow row, DataRowVersion version) +29
System.Data.BinaryNode.EvalBinaryOp( Int32 op, ExpressionNode left, ExpressionNode right, DataRow row, DataRowVersion version, Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow row, DataRowVersion version) +29
System.Data.DataExpression.Invoke(DataRow 行,DataRowVersion 版本)+151 System.Data.Index.AcceptRecord(Int32 记录,IFilter 过滤器)+103 System.Data.Index.InitRecords(IFilter 过滤器)+303 System.Data.Index ..ctor(DataTable table, Int32[] ndexDesc, IndexField[] indexFields, Comparison`1 比较, DataViewRowState recordStates, IFilter rowFilter) +491
System.Data.DataTable.GetIndex(IndexField[] indexDesc, DataViewRowState recordStates, IFilter rowFilter) + 228
System.Data.DataView.UpdateIndex(Boolean force, Boolean fireEvent) +165 System.Data.DataView.UpdateIndex(Boolean force) +12 System.Data.DataView.SetIndex2(String newSort, DataViewRowState newRowStates, IFilter newRowFilter, Boolean fireEvent) +113
System.Data.DataView.SetIndex(String newSort, DataViewRowState newRowStates, IFilter newRowFilter) +17
System.Data.DataView.set_RowFilter(String value) +160
System.Web.UI.WebControls.FilteredDataSetHelper.CreateFilteredDataView(DataTable table, String sortExpression, String filterExpression, IDictionary filterParameters) +396
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2012
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback 回调) +21
System.Web.UI.WebControls .DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +102 System.Web.UI.Control .PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI .Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint) +2496
此致 克里斯托弗·维塔利斯·彼得森