-1

I try to register an account but all it says is Object reference not set to an instance of object at the dsNewRow = ds.Tables("Username").NewRow() part.

Here's the code listing.

    ElseIf inc <> -1 Then

        If Not ExistAccount(txtUsername.Text, txtPassword.Text) Then

            Dim cb As New OleDb.OleDbCommandBuilder(da)

            Dim dsNewRow As DataRow

            dsNewRow = ds.Tables("Username").NewRow()

            dsNewRow.Item(1) = txtUsername.Text
            dsNewRow.Item(2) = txtPassword.Text

            ds.Tables("Username").Rows.Add(dsNewRow)

            da.Update(ds, "Username")
            txtUsername.Clear()
            txtPassword.Clear()

            MsgBox("Your account has been created.")

        End If

    End If

End Sub
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178

3 Answers3

0

If you are getting the error chances are the ds.Tables("Username") doesnt exist therefore calling a new row on a null DataTable will result in an exception. Check to ensure that the DataTabe "Username" is not null before calling the NewRow() method.

Something like (this is just an idea)

Dim dsNewRow As DataRow

If ds.Tables("Username") Is Nothing Then
    Dim dt As DataTable = New DataTable
    dt.Columns.Add("Username", GetType(String))
    dt.Columns.Add("Password", GetType(String))

    ds.Tables.Add(dt)

End If

dsNewRow = ds.Tables("Username").NewRow()

Now the whole section about creating a new table is optional and up to you if that's the direction you would like to go with if the table does not exist. Again the main point is changes are the "Username" table does not exist.

Cheers.

Nico
  • 12,493
  • 5
  • 42
  • 62
0

Since DataRow type is an object, you might try

     SET  dsNewRow = ds.Tables("Username").NewRow()
donPablo
  • 1,937
  • 1
  • 13
  • 18
-1

You have to use the New keyword to instanciate the Datarow as:

Dim dsNewRow As New DataRow
SomeNickName
  • 511
  • 6
  • 32