views:

421

answers:

3

Why my set of codes didn't update in DataSet? Then it goes to Error. Please anyone check this code and point me out where I am missing. Thanks in advance!

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=username;Password=pwds")

    Dim dadPurchaseInfo As New SqlDataAdapter
    Dim dsPurchaseInfo As New DataSet1
    Try
        Dim dRow As DataRow

        conxMain.Open()

        Dim cmdSelectCommand As SqlCommand = New SqlCommand("SELECT * FROM Stock", conxMain)
        cmdSelectCommand.CommandTimeout = 30

        dadPurchaseInfo.SelectCommand = cmdSelectCommand
        Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)

        dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")


        For Each dRow In dsPurchaseInfo.Tables("Stock").Rows
            If CInt(dRow.Item("StockID").ToString()) = 2 Then
                dRow.Item("StockCode") = "Re-Fashion[G]"
            End If

        Next
        dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")

    Catch ex As Exception
        MsgBox("Error : ")
    Finally
        If dadPurchaseInfo IsNot Nothing Then
            dadPurchaseInfo.Dispose()
        End If

        If dsPurchaseInfo IsNot Nothing Then
            dsPurchaseInfo.Dispose()
        End If

        If conxMain IsNot Nothing Then
            conxMain.Close()
            conxMain.Dispose()
        End If
    End Try
End Sub
+1  A: 

Does your dataAdapter has update command ?

(it looks like it doesn't - so it doesn't know what do to with update....)

Here is an Update Command example:(for an employee table with 3 columns - as listed below:

UPDATE [Employee] SET [name] = @name, [manager] = @manager WHERE (([id] = @Original_id) AND ((@IsNull_name = 1 AND [name] IS NULL) OR ([name] = @Original_name)) AND ((@IsNull_manager = 1 AND [manager] IS NULL) OR ([manager] = @Original_manager)));

SELECT id, name, manager FROM Employee WHERE (id = @id)

You can see it is a general update that can handle changes in any field.

Dani
How about this part? For Each dRow In dsPurchaseInfo.Tables("Stock").Rows If CInt(dRow.Item("StockID").ToString()) = 2 Then dRow.Item("StockCode") = "Re-Fashion[G]" End If Next It says to update dataAdapter, isn't it?
RedsDevils
You gave the adapter a select command (check your code) you need to give it an update command.your code changes the data - but the adapter need to know what command to run to update it. you can use visual studio wizard to generate an update command, or write it yoursefl.
Dani
dadPurchaseInfo.UpdateCommand = .....
Dani
Dim cmdUpdateCommand As SqlCommand = New SqlCommand("UPDATE Stock SET StockCode='Re-Fashion(H)' WHERE StockID=2", conxMain) cmdUpdateCommand.CommandTimeout = 30dadPurchaseInfo.UpdateCommand = cmdUpdateCommandDim cbUpdate As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")-------------I write like above and run but it doesn't work.
RedsDevils
Can you show me the code snippet that call after I created Update Query in Visual Studio?
RedsDevils
I've added an example of an update command. (general one)
Dani
+1  A: 

Does your condition in the loop get executed (set a break point!)? Where is the error thrown? What error?

Also, why does it use ToString at all? This seems redundant.

If CInt(dRow.Item("StockID")) = 2 Then

Should be enough.

Finally, you’re performing redundant cleanup:

If conxMain IsNot Nothing Then
    conxMain.Close()
    conxMain.Dispose()
End If

Dispose implies Close – no need to perform both operations:

Close and Dispose are functionally equivalent.

[Source: MSDN]

Konrad Rudolph
Thanks Konard Rudolph! I got it based on your error correction to my program! :) Thanks a lot. It takes me the whole day to solve! Thanks you very much!
RedsDevils
A: 

I got it from the error correction of my program by Konard Rudolph!

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=username;Password=pwds")

    Dim dadPurchaseInfo As New SqlDataAdapter
    Dim dsPurchaseInfo As New DataSet1
       Try
            Dim dRow As DataRow

            conxMain.Open()

            dadPurchaseInfo.SelectCommand = New SqlCommand("SELECT * FROM Stock", conxMain)
            Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)


            dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")

            For Each dRow In dsPurchaseInfo.Tables("Stock").Rows
                If CInt(dRow.Item("StockID")) = 2 Then
                    dRow.Item("StockCode") = "Re-Fashion(H)"
                End If

            Next
            dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")
        Catch ex As Exception
            MsgBox("Error : " & vbCrLf & ex.Message)
        Finally
            If dadPurchaseInfo IsNot Nothing Then
                dadPurchaseInfo.Dispose()
            End If

            If dsPurchaseInfo IsNot Nothing Then
                dsPurchaseInfo.Dispose()
            End If

            If conxMain IsNot Nothing Then
                conxMain.Dispose()
            End If
        End Try
  End Sub

The above set of code work to update with DataSet! Thanks to stackoverflow community and who answered my question.

Here is ref:

p.s: Like o.k.w said : The Table must have primary key. Thanks o.k.w!

RedsDevils