Results 1 to 3 of 3

Thread: Deleting Record Encounter Error When No Record Exist

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    67

    Deleting Record Encounter Error When No Record Exist

    Hello EveryBody,

    The code below encounter error when no record is existing on the table. Do I need To make another query to determine if the record exist before issuing the SQL delete command ? Thank U.

    VB Code:
    1. Public Function DeleteCreditCard(ByVal xBranchCode As String, ByVal xTrans_No As String) As String
    2.         Dim objKeyCommand As SqlCommand
    3.         Dim objConn As SqlConnection
    4.         Dim objDA As SqlDataAdapter
    5.         Dim strSQL As String
    6.  
    7.         objConn = New SqlConnection("SERVER=SirlitzServer;Database=dbSirlitz;UID=sa")
    8.         objKeyCommand = New SqlCommand
    9.         strSQL = "DELETE FROM arCreditCard WHERE BranchCode = '" & CInt(xBranchCode) & "' AND Trans_No = '" & CInt(xTrans_No) & "'"
    10.         objConn.Open()
    11.         objKeyCommand.CommandText = strSQL
    12.         objKeyCommand.CommandType = CommandType.Text
    13.         objKeyCommand.Connection = objConn
    14.         objKeyCommand.ExecuteNonQuery()
    15.         objKeyCommand = Nothing
    16.         objConn.Close()

  2. #2
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: Deleting Record Encounter Error When No Record Exist

    Several things I notice, but none of them would cause an error. Change your code to this:
    VB Code:
    1. Public Function DeleteCreditCard(ByVal xBranchCode As String, ByVal xTrans_No As String) As String
    2.         Dim objKeyCommand As SqlCommand
    3.         Dim objConn As SqlConnection
    4.         Dim objDA As SqlDataAdapter 'Delete this if it isn't being used
    5.         Dim strSQL As String
    6.  
    7.         objConn = New SqlConnection("SERVER=SirlitzServer;Database=dbSirlitz;UID=sa")
    8.        
    9.         objKeyCommand = New SqlCommand
    10.         strSQL = "DELETE FROM arCreditCard WHERE BranchCode = '" & xBranchCode & "' AND Trans_No = '" & xTrans_No & "'"
    11.         objKeyCommand.CommandText = strSQL
    12.         objKeyCommand.CommandType = CommandType.Text
    13.         objKeyCommand.Connection = objConn
    14.  
    15.         Try
    16.             objConn.Open()
    17.             objKeyCommand.ExecuteNonQuery()
    18.         End Try
    19.         Finally
    20.             objConn.Close()
    21.         End Finally
    You do not need to convert your string arguments to integers before concatenating them to the SQL string unless you are trying to remove extra non-digit characters from the string. A better method would be to have the DeleteCreditCard function take Integers as arguments, forcing the caller to perform a check on the input values to ensure they are numbers.

    Tell us exactly what exception was thrown. We need to know the type of exception and it's message. Anytime there is an error in your code you have to tell us what it is, we cannot run a snippet of your code and reproduce the error. Also, there is no harm in executing a DELETE statement with no records in the table.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting Record Encounter Error When No Record Exist

    Do the BranchCode and Trans_No columns contain numbers? If so then you shouldn't be putting single quotes around the values. That will check for records that have text values in those columns, of which there will be none, of course. That's just another example of why parameters should be used when inserting values into SQL statements.

    As DNA says, executing a DELETE statement will not produce any errors because there are no matching rows, so your error is caused by another issue. If you had told us what the error message was we could probably work out what it is. Please don't say your code produces an error without quoting the error message. Thats' like telling a doctor your sick without describing your symptoms and expecting a diagnosis.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width