views:

261

answers:

1

I'm new to vb.net and having quite a challenge with updating a field in a table using the For...Next construct on a data set. Sample code below - can anyone tell me what I'm missing? I know this particular example could be accomplished with a simple SQL update statement, but the actual use for what this code will become requires stepping through each record in the dataset - the example below is just a simple one to let me get the procedure down. Note that this runs without an exception, but just doesn't change the data in the table.

Help?! :)

TIA!

'******************************************************************* Dim objConn As SqlConnection = New SqlConnection("Data Source=DALLAS\;Initial Catalog=Adaptive;Integrated Security=True")

    Dim selectCMD As SqlCommand = New SqlCommand("SELECT * from dwbprocref", objConn)
    selectCMD.CommandTimeout = 30

    Dim custDA As SqlDataAdapter = New SqlDataAdapter
    custDA.SelectCommand = selectCMD

    Dim custCB As SqlCommandBuilder = New SqlCommandBuilder(custDA)
    custCB.QuotePrefix = "["
    custCB.QuoteSuffix = "]"


    Dim dRow As DataRow
    Dim dTable As DataTable

    objConn.Open()

    Dim custDS As DataSet = New DataSet
    custDA.Fill(custDS, "dwbprocref")

    For Each dRow In custDS.Tables(0).Rows
        If dRow.Item("pr_format") = "MDV" Then
            dRow.Item("pr_tester") = "X"
        End If
        custDS.AcceptChanges()
    Next
    custDA.Update(custDS, "dwbprocref")

    objConn.Close()

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

+1  A: 

You're calling AcceptChanges. This marks all of the rows as being unmodified, so they are never updated in the database. Remove this call and you should be good.

Adam Robinson
That fixed the problem. You ROCK! Thank you.
Dave