Update requires a valid UpdateCommand when passed DataRow collection with modified ro

newdbo

Active member
Joined
Aug 23, 2009
Messages
25
Programming Experience
Beginner
Hi all, i'm using vb.net 2005, i have error message : "Update requires a valid UpdateCommand when passed DataRow collection with modified rows", this is my code:
VB.NET:
            strSQL = "SELECT * FROM " & tblname
            Dim sqladapter As New SqlDataAdapter(strSQL, oConnection)
            Dim DS As New DataSet
            sqladapter.Fill(DS)

            Dim DT As New DataTable

            DT = DS.Tables(0)
            For i As Integer = 0 To DT.Rows.Count - 1
                DT.Rows(i)("stanam") = stanam(DT.Rows(i)("PT"), DT.Rows(i)("KDST"), DT.Rows(i)("KDKB"), DT.Rows(i)("KDFL"), DT.Rows(i)("THTN"), DT.Rows(i)("BLTN"), thnbudget)
                sqladapter.Update(DT)
            Next
    Private Function stanam(ByVal PT As String, ByVal KDST As String, ByVal KDKB As String, ByVal KDFL As String, ByVal THTN As String, ByVal BLTN As String, ByVal thnBudget As Integer) As String
        Dim selisih As Double = 0.0
        Dim nilai As Double = 0.0
        Dim sem As Integer = 0
        Dim stanam1 As String = ""
        If BLTN <> "" Then
            If Val(BLTN) <= 6 Then
                nilai = 0.5

            Else
                nilai = 0.0
            End If
        End If

        If PT = "DAN" And KDST = "3" And KDKB = "1" And (KDFL = "08A" Or KDFL = "08E" Or KDFL = "08D" Or KDFL = "08U" Or KDFL = "08T" Or KDFL = "08C" Or KDFL = "08B") And THTN = "2008" And BLTN = "4" Then
            If ((Val(thnBudget) + nilai) - (Val(THTN))) <= 3.5 And ((Val(thnBudget) + nilai) - (Val(THTN))) > 1.5 Then
                stanam1 = "TBM" + Str(Math.Ceiling((Val(thnBudget + nilai) - (Val(THTN)))) - 1)

            ElseIf ((Val(thnBudget) + nilai) - (Val(THTN))) <= 1.5 Then
                stanam1 = "TBM1"
            Else
                stanam1 = "TM"

            End If

        ElseIf PT = "SWA" And KDST = "KM" And KDKB = "2" And (KDFL = "08C" Or KDFL = "08D" Or KDFL = "08E") And THTN = "2008" And BLTN = "6" Then
            If ((Val(thnBudget) + nilai) - (Val(THTN))) <= 3.5 And ((Val(thnBudget) + nilai) - (Val(THTN))) > 1.5 Then
                stanam1 = "TBM" + Str(Math.Ceiling((Val(thnBudget + nilai) - (Val(THTN)))) - 1)

            ElseIf ((Val(thnBudget) + nilai) - (Val(THTN))) <= 1.5 Then
                stanam1 = "TBM1"
            Else
                stanam1 = "TM"

            End If

        Else

            If ((Val(thnBudget) + nilai) - (Val(THTN))) <= 2.5 And ((Val(thnBudget) + nilai) - (Val(THTN))) > 0.5 Then
                stanam1 = "TBM" + Str(Math.Ceiling((Val(thnBudget + nilai) - (Val(THTN)))))

            ElseIf ((Val(thnBudget) + nilai) - (Val(THTN))) <= 0.5 Then
                stanam1 = "TBM1"
            Else
                stanam1 = "TM"

            End If
        End If


        Return stanam1

    End Function
I want to update my datatable into my database, i have no primary key, which i dont set it since the database is imported from another format. Any help appreciated!
Thanks in advance:D
 
You DataAdapter cannot update a record if you don't provide a SQL UPDATE statement. In order to do that you need to be able to uniquely identify the record you want to update. How do you propose to do that without a primary key? It is possible but you need some column or set of columns that is guaranteed to be unique in each row.
 
It appears that you are selecting all the records and then modifying them. Instead of writing the logic in .NET, why not write the UPDATE logic in SQL itself and run as a Stored Procedure?
 
IF I'm actually reading this correctly;

01) You selecting all records and columns from a table in a different datasource into a dataset named (DS)

02) You are then creating a brand new unnamed DataTable (DT). In DT you are creating a new record for every record in the source table. Each of these new records contains only a single column of information.

03) You are then using the same dataadapter, that holds only a Select statement to your source table and trying to update this new target table to an unknown datasoure, unnamed table, by calling the update method when theres no update commands for either table.

I guess my questions start by asking, where you are trying to store these new values? Are they actually supposed going to a new table (insert for new records) or are you trying only to add an additional column to your source table?
 
No, i'm just trying to add a new value for a column, and i think the best answer right now is from jmcilhiney, because it's simpler. Thanks all, great answers.it works
 
Back
Top