Incorrect syntax near the keyword 'Key'. Error in ASP.net DetailsView

0

I set up a DetailsView with asp.net. I enabled editing, inserting, and deleting. In the editing and inserting options when I finally submit I get this:

Incorrect syntax near the keyword 'Key'.

with a stacktrace of:

[SqlException (0x80131904): Incorrect syntax near the keyword 'Key'.]

System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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 shouldCacheForAlwaysEncrypted) +262
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2698
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource1 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 command, DataSourceOperation operation) +400
System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values) +419
System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +100
System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +380
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, 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 args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +127
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +168
System.Web.UI.WebControls.LinkButton.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(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696

The code it automatically generated is this:

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

I have no idea what the issue is and I couldn't find what I was looking for in other problems. I just used the autotools from asp.net and a webform. I was following a guide from a Youtube tutorial for it.

c#
asp.net
asked on Stack Overflow Apr 22, 2021 by Arthur Dagan • edited Apr 22, 2021 by marc_s

1 Answer

0

Your column name (Story Key) contains a "space" char, ref this article https://docs.microsoft.com/en-us/sql/odbc/microsoft/column-name-limitations?view=sql-server-ver15 for the solution.

answered on Stack Overflow Apr 22, 2021 by thangcao

User contributions licensed under CC BY-SA 3.0