Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

VS 2010 VB.Net write to OleDB Issue: First Pass through loop works but all consecutive don't.

$
0
0
Greetings,

I'm working on a project that takes input from an arraylist and places that in a table in MS Access. One record insert works but when I loop through(manually changing the ID which is PK field) all consecutive records give me an error stating that the operation cannot be completed because there would be a duplicate index or primary key. The variable I'm using to increment the ID's is working properly but it doesn't seem to be setting in the Command.Parameter. So for whatever reason the record isn't moving onto the next one. Am I supposed to be doing something other than setting the ID field? Is there something that inserts to the next record that I'm missing?

Code:

       
        'loop to iterate through arraylist for insertion to DB
        For i As Integer = 0 To (arraylistVariable.Count - 1)

            'gets maximum ID number; Works Correctly
            maxId = maxIDCountCommand.ExecuteScalar()
           
            'Logic to handle Null and Over 10 Records; Works Correctly
            If maxId Is DBNull.Value Then

                maxId = 0

            ElseIf maxId > 10 Then

                DeleteTableRecords()
                maxId = 0

            End If

            'turns array item into string; Works Correctly
            strPart = arraylistVariable(i)
         
           
            'Runs string split and returns array; Works Correctly
            strArrayVar = SplitObj.Input(strPart)

            'variable used to set the ID field; Works correctly
            newID = maxId + 1

           
            If maxId < 10 Then
                'logic to predict overage of 10 records; Seems to work properly
                If newID + (arraylistVariable.Count) > 10 Then
                    DeleteRecords()

                End If
                '
                'This is the Issue Area. For whatever Reason the newID Parameter works the first time through but does Not work the times after even though it shows as changed properly.
                '
                '
                objCommand.Connection = objConnection
                objCommand.CommandText = "INSERT INTO operationsT (ID, fieldOne, fieldTwo, fieldThree, fieldFour, fieldFive) VALUES(@ID, @fieldOne, @fieldTwo, @fieldThree, @fieldFour, @fieldFive)"

                objCommand.Parameters.AddWithValue("@ID", newID) 'Issue here?
                objCommand.Parameters.AddWithValue("@fieldOne", CType(strArrayVar(0), Double))
                objCommand.Parameters.AddWithValue("@fieldTwo", strArrayVar(1))
                objCommand.Parameters.AddWithValue("@fieldThree", CType(strArrayVar(2), Double))
                objCommand.Parameters.AddWithValue("@fieldFour", strArrayVar(3))
                objCommand.Parameters.AddWithValue("@fieldFive", CType(strArrayVar(4), Double))

                'Msgbox that shows that newId does in fact change properly but on ExecuteNonQuery it isn't being utilized properly on the consecutive passes through.
                MessageBox.Show("newID just before ExecuteNonQuery:  " & newID.ToString)
                Try

                    objCommand.ExecuteNonQuery()

I'm still new to utilizing ADO so any advice is welcome. Thank you for your time and assistance.

KV

Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>