0
 Dim ColNames(7) As String
 Dim Values(7) As Object
 Dim now As DateTime = DateTime.Now()

 ColNames(0) = "Int1"
 ColNames(1) = "Int2"
 ColNames(2) = "String1"
 ColNames(3) = "String2"
 ColNames(4) = "String3"
 ColNames(5) = "String4"
 ColNames(6) = "String5"
 ColNames(7) = "String6"

 Values(0) = intVar1
 Values(1) = intVar2
 Values(2) = strVar1
 Values(3) = strVar2
 Values(4) = strVar3
 Values(5) = strVar4
 Values(6) = strVar5
 Values(7) = strVar6

 Dim goodToInsert As Boolean = True

 For i As Integer = 0 To 7
     If Values(i) = Nothing OrElse Len(Values(i)) = 0 Then
         goodToInsert = False
     End If
 Next


 If goodToInsert Then
     accessDatabase.InsertIntoTable("MyTable", ColNames, Values)
 End If

This part of the code handles gathering data to insert into my access database.

    Public Sub InsertIntoTable(ByRef TableName As String, ByRef ColumnName() As String, ByRef KeyValue() As Object)

'ColumnName and KeyValue need to have the same number of elements
'be careful when attemping to insert a value into the PrimaryKey Column as it may be of a type that cannot but manually changed

'INSERT NEW DATA INTO A TABLE
'   INSERT INTO {tablename} ([{columnname1}], [{columnname2}], [{columnname3}], ...) VALUES ('{string}', {number}, {boolean}, ...), oledbconnection

Dim ColumnString As String = vbNullString
Dim KeyString As String = vbNullString

For i As Integer = 0 To ColumnName.GetUpperBound(0)
    'build the column names part of the string
    If i <> ColumnName.GetUpperBound(0) Then
        ColumnString = ColumnString & "[" & ColumnName(i) & "], "
    Else
        ColumnString = ColumnString & "[" & ColumnName(i) & "]"
    End If
    'build the values part of the string
    Dim TempValue As String = vbNullString

    If KeyValue(i) <> Nothing Then
        If KeyValue(i).GetType.ToString = "System.String" Then
            TempValue = "'" & KeyValue(i) & "'"
        Else
            TempValue = KeyValue(i)
        End If
        If i <> KeyValue.GetUpperBound(0) Then
            KeyString = KeyString & vbNullString & TempValue & ", "
        Else
            KeyString &= TempValue
        End If
    Else
        Debug.Print("Nothing")
        If i <> KeyValue.GetUpperBound(0) Then
            KeyString &= ", "
        End If
    End If
Next

Dim con As New OleDbConnection
Dim da As New OleDbDataAdapter
Dim sql As New OleDbCommand

con.ConnectionString = Connection()
con.Open()

Dim commandText As String = "INSERT INTO " & TableName & " (" & ColumnString & ") VALUES (" & KeyString & ")"

Try
    sql.Connection = con
    sql.CommandText = commandText
    da.InsertCommand = sql
    da.InsertCommand.ExecuteNonQuery()
Catch ex As Exception
    Debug.Print(ex.ToString)

End Try

con.Close()

End Sub

This is the InsertIntoTable function.

The function runs properly and inserts data into my table correctly, but in my debug output I keep noticing this error on every call to da.InsertCommand.ExecuteNonQuery():

First-chance exception at 0x761EC42D in 3024 Card Sorter.exe: Microsoft C++ exception: int at memory location 0x003ED1EC. First-chance exception at 0x761EC42D in 3024 Card Sorter.exe: Microsoft C++ exception: int at memory location 0x003ED1EC.

I was able to use the Visual Studio debugger to narrow it down to ExecuteNonQuery() being where it occurs, but I'm having trouble understanding what it means, and how to go about resolving it. I've attempted changing my intVars to Strings with no luck to resolving the exception, and I'm also extremely curious why this isn't being caught by my Try...Catch.

AntiTcb
  • 609
  • 4
  • 15
  • You would be better off with concatenating the columns and values with `String.Join`! – OneFineDay May 14 '15 at 16:44
  • You don't see why you need `ByRef` in that Sub. You just need the values, `ByVal` will work just fine. – OneFineDay May 14 '15 at 16:47
  • ByRef was done as a minor optimization idea I read about here: [link](http://www.aivosto.com/vbtips/stringopt.html#param) – AntiTcb May 14 '15 at 16:50
  • [What is a "first chance exception"?](http://stackoverflow.com/q/564681/62576). An [answer to another question](http://stackoverflow.com/a/680262/62576) might be of interest as well; it's rather short but very succinct. – Ken White May 14 '15 at 16:51

0 Answers0