Klaas-Jelle Ras
Klaas-Jelle Ras

Reputation: 125

VB.Net - Count +1 every time when creating a new row in database

I want some sort of label that is counting every record that is imported in the database.

Do I need to use a for each loop?

Can someone explain me how to do it, or set me on the right way to do it?

With cmd

                .CommandText = "INSERT INTO Workflow ([Import], [DossierPad]) VALUES ('" + Import + "','" + Pad + "')"
                .ExecuteNonQuery()

                .CommandText = "INSERT INTO Data ([DossierPad], [Tif], [Grootte]) VALUES ('" + Pad + "','" + stukjes(0) + "','" + stukjes(2) + "')"
                .ExecuteNonQuery()

                If Tifcheck(Tif) = False Then
                    cmd.CommandText = "Update Data Set Tif = '" & Tif & "' WHERE Tif="
                ElseIf Tifcheck(Tif) = True Then

                End If

                If stukjes(2) < 20000 Then
                    .CommandText = "UPDATE Data SET Blanco = '" & blanco & "' WHERE DossierPad = '" & Pad & "'"
                    .ExecuteNonQuery()
                Else
                    .CommandText = "UPDATE Data SET Blanco = '" & blanco1 & "' WHERE DossierPad = '" & Pad & "'"
                    .ExecuteNonQuery()
                End If

End With

This is the part of code where I insert records in my database. Now my question is how can I get a label to count every record in the database while it is intering.

Upvotes: 1

Views: 1162

Answers (1)

Martin Verjans
Martin Verjans

Reputation: 4796

As specified in the MSDN docs, the method ExecuteNonQuery() returns the number of rows affected by your query. This is often used to check that your insert query ended up correctly.

So what you can do is declare an Integer somewhere in your code, and increment this integer with the result of the ExecuteNonQuery() method call.

At the end, you can update a Label with the value of your integer.

Some code:

'At the beginning of your update function
Dim myCounter As Integer

'...

'Whenever you run ExecuteNonQuery() on an INSERT statement
myCounter += .ExecuteNonQuery()

'...

'Finally, on the label that should display the result
myLabel.Text = "Updated " & myCounter.toString() & " rows."

Upvotes: 3

Related Questions