views:

61

answers:

1

I have two update sql statements for two SqlCommand object to update one DataTable with DataSet object to One Source Table. When I click on update button that's update only the first one and the second SqlCommand Object doesn't work. What's it? DataTable didn't support two Command object at one time? I write as follow:

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

    Dim dadStockInfo As New SqlDataAdapter
    Dim dsStockInfo As New DataSet
    Dim transStockInfo As SqlTransaction = Nothing
    Try
        conxMain.Open()

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

        dadStockInfo.FillSchema(dsStockInfo, SchemaType.Source, "Stock")

        dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")

        Dim cmdStockUpdateCmd As SqlCommand = Nothing

        cmdStockUpdateCmd = New SqlCommand("UPDATE Stock SET StockCode= 'TestCode' WHERE StockID = 4", conxMain, transStockInfo)
        dsStockInfo.Tables("Stock").Columns("StockID").ReadOnly = False
        dadStockInfo.UpdateCommand = cmdStockUpdateCmd

        //dadStockInfo.Update(dsStockInfo, "Stock") [I have already tried this but did not work]

        Dim cmdStockUpdateCmd1 As SqlCommand = Nothing

        cmdStockUpdateCmd1 = New SqlCommand("UPDATE Stock SET StockCode= 'Test-Code2' WHERE StockID = 1", conxMain, transPurchaseInfo)

       dadStockInfo.UpdateCommand = cmdStockUpdateCmd1

       dadStockInfo.Update(dsStockInfo, "Stock")

       transStockInfo.Commit()

Can't I use like above? please point me out !

Hello ! Help me! F1 :)

+2  A: 

The way you use it you shouln't need the dataadapters and datasets...
You just execute 2 update statements.

You can just do

cmdStockUpdateCmd1 = New SqlCommand("UPDATE Stock SET StockCode= 'Test-Code2' WHERE StockID = 1", conxMain, transPurchaseInfo)
cmdStockUpdateCmd1.ExecuteNonQuery();

Datasets and dataadapters are for getting data to the client.
Changing data on the client side and then sending the modifications to the server.
You are making your changes directly on the server with SQL.

Julian de Wit
Thanks juliandewit!
RedsDevils