locked
Error deleting record RRS feed

  • Question

  • User-1753299103 posted

    I have gone over this over and over and I do not understand as to why I am receiving this error when I try to delete a record:

    var rsdelete = db.QuerySingle("delete form teflpartner where idteflschool=@0", vblidteflschool);

    Server Error in '/' Application.


    Incorrect syntax near 'teflpartner'.

    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: Incorrect syntax near 'teflpartner'.

    Source Error: 

    Line 32:             if (rscheck == 0)
    Line 33:             {
    Line 34:                    var rsdelete = db.QuerySingle("delete form teflpartner where idteflschool=@0", vblidteflschool);
    Line 35:                   
    


    Source File: c:\Users\Simon\source\repos\Serious_V2017\account\cps\deletepartnertefl.cshtml    Line: 34 

    Stack Trace: 

    [SqlException (0x80131904): Incorrect syntax near 'teflpartner'.]
       System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2551562
       System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5951112
       System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285
       System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4169
       System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58
       System.Data.SqlClient.SqlDataReader.get_MetaData() +89
       System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted) +430
       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) +2598
       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) +1483
       System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +64
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +240
       System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
       System.Data.Common.DbCommand.ExecuteReader() +14
       WebMatrix.Data.<QueryInternal>d__22.MoveNext() +137
       System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +121
       WebMatrix.Data.Database.QuerySingle(String commandText, Object[] args) +51
       ASP._Page_account_cps_deletepartnertefl_cshtml.Execute() in c:\Users\Simon\source\repos\Serious_V2017\account\cps\deletepartnertefl.cshtml:34
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
       System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
       System.Web.WebPages.WebPage.ExecutePageHierarchy() +131
       System.Web.WebPages.StartPage.RunPage() +17
       System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
       System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +116

    Thursday, August 9, 2018 6:49 PM

Answers

  • User753101303 posted

    Hi,

    You have written delete FORM rather than delete FROM  and as the FROM keyword is optional, SQL sees FORM as a table name and doesn't know what to do with "teflpartner".

    Also from the method name it seems QuerySingle might expect to read back a single row ? If you run into an issue with that, you should use a method that is expected to read back an integer result (DELETE returns how many rows were deleted).

    Edit: it seems it would be rather https://docs.microsoft.com/en-us/previous-versions/aspnet/web-frameworks/gg569254(v%3dvs.111) ie Execute rather than QuerySingle.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2018 9:32 PM

All replies

  • User753101303 posted

    Hi,

    You have written delete FORM rather than delete FROM  and as the FROM keyword is optional, SQL sees FORM as a table name and doesn't know what to do with "teflpartner".

    Also from the method name it seems QuerySingle might expect to read back a single row ? If you run into an issue with that, you should use a method that is expected to read back an integer result (DELETE returns how many rows were deleted).

    Edit: it seems it would be rather https://docs.microsoft.com/en-us/previous-versions/aspnet/web-frameworks/gg569254(v%3dvs.111) ie Execute rather than QuerySingle.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2018 9:32 PM
  • User-1753299103 posted

    Thank you,

    I can't believe I missed that.

    Thursday, August 9, 2018 9:58 PM