Results 1 to 5 of 5

Thread: Insert and Update failed

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Insert and Update failed

    Please tell me what's wrong with the following code:

    Private Sub txtResult_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtResult.DragDrop
    popAmount()
    Dim strHolder As String
    Dim strName As String
    Dim strSSNsubstring As String
    Dim strSeperator As String
    Dim tb As TextBox = CType(sender, TextBox)
    Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath
    Dim strSQL, strSQLInsert As String
    Dim rdr As OleDbDataReader
    Dim cn As New OleDbConnection(strConn)
    Dim cmd As OleDbCommand = cn.CreateCommand()
    Dim command As OleDbCommand = cn.CreateCommand()

    strName = tb.Name
    strName = tb.Text()
    strSSNsubstring = strName.Substring(5, 9)
    strSSNtext = CInt(strSSNsubstring)
    strSeperator = "*************************************************"

    strHolder = lstApplicant.Items(lstApplicant.SelectedIndex).ToString()

    tb.Text = strName & vbCrLf & strSeperator & vbCrLf & "Awarded: " & strHolder & " scholarship" & _
    vbCrLf & "SSN#: " & strSSNtext & vbCrLf & "Amount Award: $" & strAmount
    cn.Open()
    strSQL = "SELECT * FROM Awarded WHERE awrdID=" & strSSNtext
    Dim cmdSQL As New OleDbCommand(strSQL, cn)

    Try
    cmdSQL.ExecuteReader()
    cn.Close()
    cn.Open()
    cmd.CommandText = "UPDATE Awarded SET awrdName=" & "'" & strHolder & "'" & " awrdAmount =" & strAmount & " WHERE awrdID=" & strSSNtext
    cmd.ExecuteNonQuery()
    Catch dbException As Exception
    MessageBox.Show(dbException.Message)
    Dim strError As String = dbException.Message.ToString()
    If strError = "No data exists for the row/column." Then
    cn.Close()
    cn.Open()
    strSQLInsert = "INSERT INTO Awarded (awrdID, awrdName, awrdAmount) (strSSNtext, 'strHolder', 'strAmount')"
    Dim cmdQuery As New OleDbCommand(strSQLInsert, cn)
    cmdQuery.ExecuteNonQuery()
    End If
    End Try
    cn.Close()
    End Sub
    What I want to do is to either Update the data in the database if it's already exist or Insert if the account does not exist yet. Here's the problem,there is no single data in the table name Awarded, but somehow the cmdSQL.ExecuteReader()doesn't generate an error of "No data exists for the row/column." so the program will jump to the Catch Exception section and do an Insert since there is no account exist in the Award table yet.

    Many thanks in advance!

    Chong

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Sorry thats not how the ExecuteReader bit works, but there is a better way to do this:
    VB Code:
    1. Private Sub txtResult_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtResult.DragDrop
    2.     popAmount()
    3.     Dim strHolder As String
    4.     Dim strName As String
    5.     Dim strSSNsubstring As String
    6.     Dim strSeperator As String
    7.     Dim tb As TextBox = CType(sender, TextBox)
    8.     Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath
    9.     Dim strSQL, strSQLInsert As String
    10.     Dim rdr As OleDbDataReader
    11.     Dim cn As New OleDbConnection(strConn)
    12.     Dim cmd As OleDbCommand = cn.CreateCommand()
    13.     Dim command As OleDbCommand = cn.CreateCommand()
    14.  
    15.     strName = tb.Name
    16.     strName = tb.Text()
    17.     strSSNsubstring = strName.Substring(5, 9)
    18.     strSSNtext = CInt(strSSNsubstring)
    19.     strSeperator = "*************************************************"
    20.  
    21.     strHolder = lstApplicant.Items(lstApplicant.SelectedIndex).ToString()
    22.  
    23.     tb.Text = strName & vbCrLf & strSeperator & vbCrLf & "Awarded: " & strHolder & " scholarship" & _
    24.     vbCrLf & "SSN#: " & strSSNtext & vbCrLf & "Amount Award: $" & strAmount
    25.     cn.Open()
    26.     'change it to return the count of records found
    27.     strSQL = "SELECT COUNT(*) FROM Awarded WHERE awrdID=" & strSSNtext
    28.     Dim cmdSQL As New OleDbCommand(strSQL, cn)
    29.  
    30.     Try
    31.         'just get single value and there is no need to close the connection if you are going to use it again
    32.         Dim count As Integer = cmdSQL.ExecuteScalar
    33.         If count > 0 Then
    34.             'some records found so update
    35.             cmd.CommandText = "UPDATE Awarded SET awrdName=" & "'" & strHolder & "'" & " awrdAmount =" & strAmount & " WHERE awrdID=" & strSSNtext
    36.             cmd.ExecuteNonQuery()
    37.         Else
    38.             'no records found so add
    39.             strSQLInsert = "INSERT INTO Awarded (awrdID, awrdName, awrdAmount) (strSSNtext, 'strHolder', 'strAmount')"
    40.             Dim cmdQuery As New OleDbCommand(strSQLInsert, cn)
    41.             cmdQuery.ExecuteNonQuery()
    42.         End If
    43.     Catch dbException As Exception
    44.         MessageBox.Show(dbException.Message)
    45.  
    46.     Finally
    47.         cn.Close()
    48.     End Try
    49. End Sub

    Also when posting code enclose it within a code block instead of a quote one and its easier to read. [ vbcode ][ /vbcode ] without the spaces in the brackets.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Insert and Update failed

    Thank you very much for the help. I have an error on the Insert query. Here's the error message.

    Syntax error in INSERT INTO Statement.

    What could this be?

    Chong

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Insert and Update failed

    The Syntax error is because I missed the Values in front of the parenthesis. However, after I put in the word Values in, I got this error No value given for one or more required paramenters. Is this mean the parameters I'm passing contains nothing? I did a debug and found that the Insert query parameters of strSSNtext, 'strHolder', 'strAmount' doesn't show the actually value instead it only show the variable name.

    Chong

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The syntax of your INSERT sql is wrong. I think you forgot the word 'VALUES'.

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