首页 > 解决方案 > 关键字“Key”附近的语法不正确。ASP.net 详细信息视图中的错误

问题描述

我用 asp.net 设置了一个 DetailsView。我启用了编辑、插入和删除。在我最终提交时的编辑和插入选项中,我得到了这个:

关键字“Key”附近的语法不正确。

堆栈跟踪:

[SqlException(0x80131904):关键字'Key'附近的语法不正确。]

System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,Boolean breakConnection,Action 1 wrapCloseInAction) +2582782 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)+6033430
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+297
System.Data.SqlClient.TdsParser .TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4291
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlways ) +262
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,布尔异步,Int32 超时,任务和任务,布尔异步写入,布尔 inRetry,SqlDataReader ds,布尔 describeParameterEncryptionRequest)+2698
System.Data.SqlClient.SqlCommand。 RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +1611 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +390
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() + 297
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand命令,DataSourceOperation操作)+400
System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary 值)+419
System.Web.UI.DataSourceView.Insert(IDictionary 值,DataSourceViewOperationCallback 回调)+100
System.Web.UI.WebControls.DetailsView.HandleInsert(字符串 commandArg, Boolean CauseValidation) +380
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean CauseValidation, String validationGroup) +647
System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +92
System. Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +88
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs参数)+37
System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e) +127
System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument) +168
System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI。 IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9858668
System.Web.UI.Page。 ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+1696

它自动生成的代码是这样的:

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"  
     AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="UserDataSet" 
     Height="50px" Width="125px">
    <Fields>
        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
        <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
        <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
        <asp:CheckBoxField DataField="Admin" HeaderText="Admin" SortExpression="Admin" />
        <asp:BoundField DataField="Story Key" HeaderText="Story Key" SortExpression="Story Key" />
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="UserDataSet" runat="server" 
     ConnectionString="<%$ ConnectionStrings:UserDBConnectionString %>" 
     DeleteCommand="DELETE FROM [UserTable] WHERE [Id] = @Id" 
     InsertCommand="INSERT INTO [UserTable] ([Id], [Username], [Password], [Admin], [Story Key]) VALUES (@Id, @Username, @Password, @Admin, @Story_Key)" 
     SelectCommand="SELECT * FROM [UserTable]" 
     UpdateCommand="UPDATE [UserTable] SET [Username] = @Username, [Password] = @Password, [Admin] = @Admin, [Story Key] = @Story_Key WHERE [Id] = @Id">
     <DeleteParameters>
         <asp:Parameter Name="Id" Type="Int32" />
     </DeleteParameters>
     <InsertParameters>
         <asp:Parameter Name="Id" Type="Int32" />
         <asp:Parameter Name="Username" Type="String" />
         <asp:Parameter Name="Password" Type="String" />
         <asp:Parameter Name="Admin" Type="Boolean" />
         <asp:Parameter Name="Story_Key" Type="String" />
     </InsertParameters>
     <UpdateParameters>
         <asp:Parameter Name="Username" Type="String" />
         <asp:Parameter Name="Password" Type="String" />
         <asp:Parameter Name="Admin" Type="Boolean" />
         <asp:Parameter Name="Story_Key" Type="String" />
         <asp:Parameter Name="Id" Type="Int32" />
     </UpdateParameters>
</asp:SqlDataSource>

我不知道问题是什么,我在其他问题中找不到我想要的东西。我刚刚使用了来自 asp.net 的自动工具和一个网络表单。我正在遵循 Youtube 教程中的指南。

标签: c#asp.net

解决方案


您的列名(故事键)包含一个“空格”字符,请参阅本文https://docs.microsoft.com/en-us/sql/odbc/microsoft/column-name-limitations?view=sql-server-ver15为解决方案。


推荐阅读