Search code examples
sqlvb.netparametersoledbms-access-2010

[VB.Net]SQL Parameters Sending Name Instead of Values


I am trying to figure out how to use command parameters to pass values through an SQL statement since concatenation was not working at all but I am running into a weird problem and I cannot find any information on it.

Whenever the SQL statement is called the names of the parameters, like @Address1, are being placed into my database instead of the values of the parameters.

Here is my SQL statement, command, and connection code:

Private Function qDBase()
    con.ConnectionString = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\pos408_w4_Team_B_Database.accdb;Persist Security Info=False" ' database should be located with the .exe file so that it can be detected and connected to
    Try 'Make sure we also wrap connection statement in try in case something fails there
        con.Open()

        Try ' Wrap each query in a try/catch so that if it fails it will not fail silently due to code in Button1_Click
            ' Always do Customer_Index first, the primary key here is a secondary key in Account_Index, via one-to-many relationship, and unless the corresponding value can be loaced in Customer_Index it will fail to insert the data
            qStr = "Insert Into `Customer_Index` Values('@Soc2','@FName','@MInitial','@LName','@DriverL','@Address1','@Address2','@City','@State','@Zip2','@Phone2','@Email');"


            ' Create and append parameters list
            com.Parameters.AddWithValue("Soc2", Soc2)
            com.Parameters.AddWithValue("FName", FName)
            com.Parameters.AddWithValue("MInitial", MInitial)
            com.Parameters.AddWithValue("LName", LName)
            com.Parameters.AddWithValue("DriverL", DriverL)
            com.Parameters.AddWithValue("Address1", Address1)
            com.Parameters.AddWithValue("Address2", Address2)
            com.Parameters.AddWithValue("City", City)
            com.Parameters.AddWithValue("State", State)
            com.Parameters.AddWithValue("Zip2", Zip2)
            com.Parameters.AddWithValue("Phone2", Phone2)
            com.Parameters.AddWithValue("Email", Email)




            com.CommandText = qStr ' Set command value to qStr
            com.Connection = con ' Set command connection
            com.ExecuteNonQuery() ' Execute the command

            qSuccesses += 1
        Catch ex As Exception
            MsgBox(ex) ' Show thrown exception to user
        End Try

        'Try
        'qStr = "Insert Into `Account_Index` Values('" & Account & "','" & Soc & "','" & AccountType & "');"

        'com.CommandText = qStr ' Set command value to qStr
        'com.ExecuteNonQuery() ' Execute the command

        '            qSuccesses += 1
        '       Catch ex As Exception
        '          MsgBox(ex) ' Show thrown exception to user
        '     End Try

    Catch ex As Exception
        MsgBox(ex) ' Show thrown exception to user
        Return False ' REturn prematurely because connection could not be made, no point going any further
    End Try

    If (qSuccesses = 1) Then ' If both try's work
        qSuccesses = 0 ' reset to zero
        Return True ' return true
    Else ' If one or both try's fail
        qSuccesses = 0 'reset to zero
        Return False ' return false
    End If
End Function

And the values that should be sent which are initialized when the form loads:

    Soc2 = "123-45-6789"
    FName = "Test"
    MInitial = "D"
    LName = "Test"
    DriverL = "Test"
    Address1 = "123 Test Rd"
    Address2 = " "
    City = "Denver"
    State = "CO"
    Zip2 = "12345-6789"
    Phone2 = "(123)456-7890"
    Email = "[email protected]"

And then the output in my Access 2010 database:

@Soc2 @FName @MInitial @LName @DriverL @Address1 @Address2 @City @State @Zip2 @Phone2 @Email

Does AddWithValue no longer work correctly or something?


Solution

  • I hate when this happens, figuring stuff out minutes after posting a question. I should not have used apostrophes in the SQL statement. Just decided to try it with out those for the hell of it and it worked perfectly this time.