Home All Groups Group Topic Archive Search About

Error adding rows to tables

Author
1 Aug 2006 7:21 AM
Peter Newman
running vb.net 2003 & SQL 2005

I have the following function that is meant to add a row to a table...  but
i keep getting an error and i dont understand why.  Ive spent a couple of
days on this now, has anybody any suggestions ?

Error :-
'row' argument cannot be null.
Parameter name: row

Code :-

    Private Manager As CurrencyManager
    Public sLicence As String
    Private Orig_Accounts As New DataSet
    Private NewRow As DataRow
    Private NewRecordFlag As Boolean = False



    Private Function ValidateChanges()
        Manager.EndCurrentEdit()
        If NewRecordFlag Then
            Try
' THIS IS WHERE THE ERROR OCCOURS
                Orig_Accounts.Tables("TBL_OrigAccounts").Rows.Add(NewRow)
            Catch DBError As Exception
                Console.WriteLine(DBError.Message)
            Exit function
            End Try
            SetNewData()
            NewRow = Orig_Accounts.Tables("TBL_OrigAccounts").NewRow
            BindControls()
        End If
        If Orig_Accounts.HasChanges(DataRowState.Modified) Then
            If MsgBox("Update changes to Licence " & sLicence & " ?",
MsgBoxStyle.OKCancel, "Profile Update") = MsgBoxResult.OK Then
                Try
                    SQL_COMMANDBUILDER.DataAdapter = SQL_DATAADAPTER
                    SQL_COMMANDBUILDER.GetUpdateCommand()
                    SQL_DATAADAPTER.Update(Orig_Accounts, "TBL_OrigAccounts")
                    Orig_Accounts.AcceptChanges()
                Catch ex As Exception
                    ' Tempory
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            If Orig_Accounts.HasChanges(DataRowState.Added) Then
                SQL_COMMANDBUILDER.DataAdapter = SQL_DATAADAPTER
                SQL_COMMANDBUILDER.GetUpdateCommand()
                SQL_DATAADAPTER.Update(Orig_Accounts, "TBL_OrigAccounts")
                Orig_Accounts.AcceptChanges()
            End If
        End If
        NewRecordFlag = False
    End Function

Author
2 Aug 2006 7:05 PM
dotNetDave
You need to do this before the Rows.Add line:

NewRow = New DataRow

======================================
David McCarter
www.vsdntips.com
VSDN Tips & Tricks .NET Coding Standards available at:
www.cafepress.com/vsdntips.20412485


Show quoteHide quote
"Peter Newman" wrote:

> running vb.net 2003 & SQL 2005
>
> I have the following function that is meant to add a row to a table...  but
> i keep getting an error and i dont understand why.  Ive spent a couple of
> days on this now, has anybody any suggestions ?
>
> Error :-
> 'row' argument cannot be null.
> Parameter name: row
>
> Code :-
>
>     Private Manager As CurrencyManager
>     Public sLicence As String
>     Private Orig_Accounts As New DataSet
>     Private NewRow As DataRow
>     Private NewRecordFlag As Boolean = False
>
>
>
>     Private Function ValidateChanges()
>         Manager.EndCurrentEdit()
>         If NewRecordFlag Then
>             Try
> ' THIS IS WHERE THE ERROR OCCOURS
>                 Orig_Accounts.Tables("TBL_OrigAccounts").Rows.Add(NewRow)
>             Catch DBError As Exception
>                 Console.WriteLine(DBError.Message)
>             Exit function
>             End Try
>             SetNewData()
>             NewRow = Orig_Accounts.Tables("TBL_OrigAccounts").NewRow
>             BindControls()
>         End If
>         If Orig_Accounts.HasChanges(DataRowState.Modified) Then
>             If MsgBox("Update changes to Licence " & sLicence & " ?",
> MsgBoxStyle.OKCancel, "Profile Update") = MsgBoxResult.OK Then
>                 Try
>                     SQL_COMMANDBUILDER.DataAdapter = SQL_DATAADAPTER
>                     SQL_COMMANDBUILDER.GetUpdateCommand()
>                     SQL_DATAADAPTER.Update(Orig_Accounts, "TBL_OrigAccounts")
>                     Orig_Accounts.AcceptChanges()
>                 Catch ex As Exception
>                     ' Tempory
>                     MsgBox(ex.Message)
>                 End Try
>             End If
>         Else
>             If Orig_Accounts.HasChanges(DataRowState.Added) Then
>                 SQL_COMMANDBUILDER.DataAdapter = SQL_DATAADAPTER
>                 SQL_COMMANDBUILDER.GetUpdateCommand()
>                 SQL_DATAADAPTER.Update(Orig_Accounts, "TBL_OrigAccounts")
>                 Orig_Accounts.AcceptChanges()
>             End If
>         End If
>         NewRecordFlag = False
>     End Function
>