简体   繁体   中英

loop through datatable and run stored procedure

I am trying to loop through a datatable, that will pass information to my stored procedure but it isn't working.

even though it works when i manuelly

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dv As New DataView
            Dim dt As New DataTable
            dv = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

            dt = dv.ToTable()


            For Each row As DataRow In dt.Rows

                If row("vtr_gen10").ToString() = "y" Or row("vn10").ToString() = "a" Or row("vtr_gen10").ToString() = "p" Then
                    Dim SQLCon As New SqlClient.SqlConnection
                    Dim sqlcmd As New SqlCommand
                    SQLCon.ConnectionString = SqlDataSource2.ConnectionString

                    SQLCon.Open()
                    sqlcmd.CommandText = "protest" ' Stored Procedure to Call
                    sqlcmd.CommandType = CommandType.StoredProcedure 'Setup Command Type
                    sqlcmd.Connection = SQLCon 'Active Connection

                    sqlcmd.Parameters.AddWithValue("@ID", row("id"))
                    sqlcmd.Parameters.AddWithValue("@Value", "Y")


                    sqlcmd.ExecuteNonQuery()
                    SQLCon.Close()

                End If

            Next

        End Sub

There is nothing seems to be wrong with your code, but if you think its because its moving too fast, try adding sleep using System.Threading.Thread.Sleep(milliseconds)

Are you absolutely sure the IF criteria is being met after the first two records? One thing to look for is spaces or other string anomalies such as carraige returns in your database...

Row("vn10").ToString for example may for some reason be "a " instead of "a". Might be worth putting in a temporary debug line such as:

Throw New Exception("[" & Row("vn10").ToString & "]") 

or inspect length on some records that erroneously fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM