我正在Asp.net中编写一个应用程序,其中c#作为代码。我有一个文本框(" txtNameSearch")用于过滤SqlDataSource的用户位置信息,以及基于该SQL数据填充的gridview。当用户在框中放置逗号或空格时,SqLDataSource会出现异常错误。我希望能够允许这种搜索,因为数据库在此字段中允许使用标点符号。
<asp:SqlDataSource ID="Sql_NameList" runat="server" ConnectionString='<%$ ConnectionStrings:Recorder %>'
SelectCommand="SELECT [Name], [Address1], [Address2], [CSZ], [PersonID] FROM [People] WHERE (CONTAINS([Name], @Name))">
<SelectParameters>
<asp:ControlParameter ControlID="txtNameSearch" PropertyName="Text" Name="Name" Type="String">
</asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
以下是例外文字:
&#39; /&#39;中的服务器错误应用
&#39;附近的语法错误,&#39;在全文搜索条件下,约翰逊,马,&#39;。
描述:执行期间发生了未处理的异常 当前的网络请求。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.Data.SqlClient.SqlException:语法错误 靠近&#39;,&#39;在全文搜索条件下,约翰逊,马,&#39;。
来源错误:
执行期间生成了未处理的异常 当前的网络请求。有关的来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈追踪:
[SqlException(0x80131904):&#39;附近的语法错误,&#39;在全文中 搜索条件&#39; Johnson,Ma&#39;。] System.Data.SqlClient.SqlConnection.OnError(SqlException异常, Boolean breakConnection,Action
1 wrapCloseInAction) +2442126
1 wrapCloseInAction) +5736904 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+628
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔&amp; dataReady)+3731
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()+58
System.Data.SqlClient.SqlDataReader.get_MetaData()+89
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString)+379
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32超时,任务&amp; task,Boolean asyncWrite,SqlDataReader ds,Boolean describeParameterEncryptionRequest)+2026
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource`1完成,Int32超时,任务&amp;任务, Boolean asyncWrite)+375
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法)+53
System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior 行为,字符串方法)+240
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior 行为)+41
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(的CommandBehavior 行为)+12 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,DataTable [] datatables,Int32 startRecord,Int32 maxRecords, String srcTable,IDbCommand命令,CommandBehavior行为)+139
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令, CommandBehavior行为)+136
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet,String srcTable)+86
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments 参数)+1494
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback回调)+22
System.Web.UI.WebControls.DataBoundControl.PerformSelect()+ 143 3 System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+74
System.Web.UI.WebControls.GridView.DataBind()+9
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()+ 114 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls()+92 System.Web.UI.Control.PreRenderRecursiveInternal()+42
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Control.PreRenderRecursiveInternal()+160
System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) 883
答案 0 :(得分:0)
I fixed this problem in the c# code behind by taking the text box and checking for a comma or space. If that exists quotation marks are added around the text and then the query does not choke on the comma or space. See code below:
if (txtNameSearch.Text.Contains(",") || txtNameSearch.Text.Contains(" "))
{
if (txtNameSearch.Text.Contains('"'))
{
}
else
{
txtNameSearch.Text = '"' + txtNameSearch.Text + '"';
}
}