benshaws
Hobbyist Programmer
.Net Framework 4.6
Visual Studio 2019
I am getting a syntax error at the
In the Locals window I can see that the
The one thing I have noticed is the
It's been a while since I last wrote any code so I must be missing something but this is code I am reusing from a past project that I thought was working. Could someone please have a look and advise?
Thanks,
Kevin.
Visual Studio 2019
VB.NET:
Public Shared Function AddRow(ByVal databaseUser As String, ByVal databasePassword As String, ByVal databasePath As String, ByVal databaseTable As String, ByVal databaseColumns As Array, ByVal databaseValues As Array) As Boolean
Dim connection As OleDb.OleDbConnection = BuildConnectionString(databaseUser, databasePassword, databasePath)
Try
If databaseColumns.Length = databaseValues.Length Then
Dim insertString As String = "INSERT INTO " + databaseTable + " ("
For Each column In databaseColumns
'insertString += $"{column}, "
insertString += column + ","
Next
insertString.TrimEnd(",")
insertString += ") VALUES("
For Each value In databaseValues
'insertString += $"'{value}', "
insertString += "'" + value + "',"
Next
insertString.TrimEnd(",")
insertString += ")"
Dim insertCommand As New OleDb.OleDbCommand With {
.CommandText = insertString,
.Connection = connection
}
Using insertCommand
If connection.State = ConnectionState.Closed Then connection.Open()
insertCommand.ExecuteNonQuery()
insertCommand.Dispose()
End Using
Return True
Else
Return False
Exit Function
End If
Catch ex As Exception
Return False
Finally
connection.Close()
End Try
End Function
I am getting a syntax error at the
insertCommand.ExecuteNonQuery()
.In the Locals window I can see that the
insertString
still includes the last ,
that the two insertString.TrimEnd(",")
code lines should be removing.The one thing I have noticed is the
insertString
is surronded by quotes - one at each end of the string. Is this the problem?It's been a while since I last wrote any code so I must be missing something but this is code I am reusing from a past project that I thought was working. Could someone please have a look and advise?
Thanks,
Kevin.