Avatar of PeterBaileyUk
PeterBaileyUk
 asked on

sql server variable names already declared in vb.net

I have made this for loop that works ok and grabs the data i need.

within the loop it wont update the table as it says variables already declared, it says this whatever I rename them too:

not sure what to do about that error.

 For Each drAccessRecord As DataRow In dtRecordsFromAccessSmmt.Rows ' each smmt veh cat record

                    '********************************************************************************************************

                    cmdInsertSmmt.CommandText = "UPDATE dbo.TblWords SET VehCategory = @VehCategory WHERE ClientCode = @ClientCode "

                    cmdInsertSmmt.Parameters.AddWithValue("@ClientCode", drAccessRecord("MVRIS CODE"))
                    cmdInsertSmmt.Parameters.AddWithValue("@VehCategory", drAccessRecord("Vehicle Category Code"))
                    cmdInsertSmmt.ExecuteNonQuery()



                Next

Open in new window

Microsoft SQL ServerVisual Basic.NET

Avatar of undefined
Last Comment
PeterBaileyUk

8/22/2022 - Mon
Scott McDaniel (EE MVE )

Try this syntax instead:

 cmdInsertSmmt.CommandText = "UPDATE dbo.TblWords SET VehCategory = @VehCategory WHERE ClientCode = @ClientCode "

cmdInsertSmmt.Parameters.Add("@ClientCode", SqlDBType.VarChar, 50).Value =  drAccessRecord("MVRIS CODE")
cmdInsertSmmt.Parameters.Add("@VehCategory",SqlDBType.VarChar, 50).value = drAccessRecord("Vehicle Category Code")

Obviously you'd change the "SqlDBType.VarChar" to reflect the actual datatypes.
ASKER CERTIFIED SOLUTION
Valliappan AN

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PeterBaileyUk

ASKER
@ Valliappan

I dont believe i have here is the part above the for loop, so does that need to go within the loop?

 Using cnSqlSmmt As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")

            Using cmdInsertSmmt As New SqlClient.SqlCommand
                cmdInsertSmmt.Connection = cnSqlSmmt
                cnSqlSmmt.Open()
                Dim y As Long

                y = dtRecordsFromAccessSmmt.Rows.Count
                For Each drAccessRecord As DataRow In dtRecordsFromAccessSmmt.Rows ' each smmt veh cat record

Open in new window

PeterBaileyUk

ASKER
its still giving the error despite those amendments

    Public Sub UpdateVehicleCat()
        Dim dtRecordsFromAccessSmmt As DataTable

        Using cnAccess As New OleDb.OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=N:\Data\SMMT\smmt master.mdb")
            Using cmdSelect As New OleDb.OleDbCommand
                cmdSelect.Connection = cnAccess
                cnAccess.Open()
                cmdSelect.CommandText = "SELECT [MVRIS CODE], [Vehicle Category Code] FROM smmt;"
                Dim dtAdpt As New OleDb.OleDbDataAdapter(cmdSelect)
                dtRecordsFromAccessSmmt = New DataTable
                dtAdpt.Fill(dtRecordsFromAccessSmmt)
            End Using
        End Using


        Using cnSqlSmmt As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")

            Using cmdInsertSmmt As New SqlClient.SqlCommand
                cmdInsertSmmt.Connection = cnSqlSmmt
                cnSqlSmmt.Open()
                Dim y As Long

                y = dtRecordsFromAccessSmmt.Rows.Count
                For Each drAccessRecord As DataRow In dtRecordsFromAccessSmmt.Rows ' each smmt veh cat record

                    cmdInsertSmmt.CommandText = "UPDATE dbo.TblWords SET VehCategory = @VehCategory WHERE ClientCode = @ClientCode "
                    cmdInsertSmmt.Parameters.Add("@ClientCode", SqlDbType.NVarChar, 50).Value = drAccessRecord("MVRIS CODE")
                    cmdInsertSmmt.Parameters.Add("@VehCategory", SqlDbType.NVarChar, 50).Value = drAccessRecord("Vehicle Category Code")
                    cmdInsertSmmt.ExecuteNonQuery()
                Next

            End Using
            cnSqlSmmt.Close()
        End Using

    End Sub

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
PeterBaileyUk

ASKER
I stepped through and the error was on the 2nd record, so maybe thats because of what  Valliappan said
PeterBaileyUk

ASKER
I believe its now working
        Using cnSqlSmmt As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")



            Dim y As Long

                y = dtRecordsFromAccessSmmt.Rows.Count
                For Each drAccessRecord As DataRow In dtRecordsFromAccessSmmt.Rows ' each smmt veh cat record
                Using cmdInsertSmmt As New SqlClient.SqlCommand
                    cmdInsertSmmt.Connection = cnSqlSmmt
                    cnSqlSmmt.Open()
                    cmdInsertSmmt.CommandText = "UPDATE dbo.TblWords SET VehCategory = @VehCategory WHERE ClientCode = @ClientCode "

                    cmdInsertSmmt.Parameters.Add("@ClientCode", SqlDbType.NVarChar, 50).Value = drAccessRecord("MVRIS CODE")
                    cmdInsertSmmt.Parameters.Add("@VehCategory", SqlDbType.NVarChar, 50).Value = drAccessRecord("Vehicle Category Code")
                    cmdInsertSmmt.ExecuteNonQuery()
                    cnSqlSmmt.Close()
                End Using
            Next

        End Using



    End Sub

Open in new window

PeterBaileyUk

ASKER
I checked and in some cases it populates in others it doesnt, the for loop has the right count.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PeterBaileyUk

ASKER
I debugged with a smaller set of access records just using one record that failed to update previously, that record updated.

it must be the sequence in the for loop with the command objects.

I just do not know what.
Valliappan AN

Is the issue resolved ?
PeterBaileyUk

ASKER
i am just running it again but it does not appear to get to the end statement I had it running for 40 minutes.

Public Sub UpdateVehicleCat()
        Dim dtRecordsFromAccessSmmt As DataTable

        Using cnAccess As New OleDb.OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=N:\Data\SMMT\smmt master.mdb")
            Using cmdSelect As New OleDb.OleDbCommand
                cmdSelect.Connection = cnAccess
                cnAccess.Open()
                cmdSelect.CommandText = "SELECT [MVRIS CODE], [Vehicle Category Code] FROM smmt where [Vehicle Category Code] is not null ORDER BY [MVRIS CODE];"
                Dim dtAdpt As New OleDb.OleDbDataAdapter(cmdSelect)
                dtRecordsFromAccessSmmt = New DataTable
                dtAdpt.Fill(dtRecordsFromAccessSmmt)
            End Using
        End Using


        Using cnSqlSmmt As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")



            Dim y As Long



            y = dtRecordsFromAccessSmmt.Rows.Count

                For Each drAccessRecord As DataRow In dtRecordsFromAccessSmmt.Rows ' each smmt veh cat record
                Using cmdInsertSmmt As New SqlClient.SqlCommand
                    cmdInsertSmmt.Connection = cnSqlSmmt
                    cnSqlSmmt.Open()
                    cmdInsertSmmt.CommandText = "UPDATE dbo.TblWords SET VehCategory = @VehCategory WHERE ClientCode = @ClientCode "
                    cmdInsertSmmt.Parameters.Add("@ClientCode", SqlDbType.NVarChar, 50).Value = drAccessRecord("MVRIS CODE")
                    cmdInsertSmmt.Parameters.Add("@VehCategory", SqlDbType.NVarChar, 50).Value = drAccessRecord("Vehicle Category Code")
                    cmdInsertSmmt.ExecuteNonQuery()


                    cnSqlSmmt.Close()




                End Using
            Next



        End Using
        MessageBox.Show("Finished Update Vehicle Category.")
    End Sub

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Valliappan AN

How many records are you trying to update, and how many is updated so far?
PeterBaileyUk

ASKER
There are just under 700000 rows so i think i will have to do this a different way as its poiible its working but its just slow.
Valliappan AN

If its one time, you may import the Access table to SQL using SQL Import/Export tool, to a table and then use joins to update it in one sql statement, will be very fast.

Another option is to use, OpenDataSource, or OpenQuery, or Linked Servers. But i would suggest above option.

Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PeterBaileyUk

ASKER
I will append the data to a smaller table, I think its almost there its the syntax thats causing an exception

  cmdInsertSmmt.CommandText = "INSERT INTO TblVehCat (ClientCode, VehCategory) VALUES ('" & drAccessRecord("MVRIS CODE") & "', '" & drAccessRecord("Vehicle Category Code") & "','" & " )"


                    cmdInsertSmmt.ExecuteNonQuery()

Open in new window

PeterBaileyUk

ASKER
finally got it with
 cmdInsertSmmt.CommandText = "INSERT INTO TblVehCat (ClientCode, VehCategory) VALUES ('" & drAccessRecord("MVRIS CODE") & "', '" & drAccessRecord("Vehicle Category Code") & "'" & " )"

Open in new window

PeterBaileyUk

ASKER
thank you that was the exact cause.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck