2016-10-19 7 views
0

코드 숨김으로 C#을 사용하여 Asp.net에 응용 프로그램을 작성하고 있습니다. SqlDataSource를 필터링하기위한 사용자 장소 정보의 텍스트 상자 ("txtNameSearch")와 해당 SQL 데이터를 기반으로 채우는 gridview가 있습니다. 사용자가 상자에 쉼표 나 공백을 입력하면 SqLDataSource에 예외 오류가 발생합니다. 데이터베이스에이 필드에 구두점이 허용되어 있기 때문에 이런 종류의 검색을 허용하고 싶습니다.SqlDataSource 및 콤마 또는 공백이 검색 문자열의 일부인 경우 "포함"

Server Error in '/' Application.

Syntax error near ',' in the full-text search condition 'Johnson, Ma'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Syntax error near ',' in the full-text search condition 'Johnson, Ma'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException (0x80131904): Syntax error near ',' in the full-text search condition 'Johnson, Ma'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) +2442126
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) +5736904 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& 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 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +240
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +12 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +139
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +136
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +86
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1494
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +22
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
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(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883

답변

0

내가 텍스트 상자를 복용하고 쉼표 또는 공간을 확인하여 뒤에 C# 코드에서이 문제를 해결 :

<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> 

다음은 예외 텍스트입니다. 존재하는 경우 따옴표가 텍스트 주위에 추가되고 쿼리가 쉼표 나 공백을 막지 않습니다. 아래 코드 참조 :

 if (txtNameSearch.Text.Contains(",") || txtNameSearch.Text.Contains(" ")) 
      { 
      if (txtNameSearch.Text.Contains('"')) 
       { 

       } 
      else 
       { 
       txtNameSearch.Text = '"' + txtNameSearch.Text + '"'; 
       } 
      }