Results 1 to 8 of 8

Thread: Adding A New Row to my DataSet :(

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Adding A New Row to my DataSet :(

    I'm trying to use the following code and it's giving me a big, big headache..

    VB Code:
    1. Dim NewRow As DataRow = DS.Tables("Users").NewRow
    2.         NewRow!UserName = txtUserName.Text
    3.         NewRow!Password = txtPassword.Text
    4.         NewRow!Type = "User"
    5.         DS.Tables("Users").Rows.Add(NewRow)
    6.  
    7.         Dim CB As New OleDbCommandBuilder(DA)
    8.         DA.UpdateCommand = CB.GetUpdateCommand
    9.         DA.Update(DS, "Users")

    This works just fine if i don't add the record

    is there anything I need to do between adding the record and updating the thing? Thanks.


    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    If you add the record what happen? Does your datatable have primary key? Does the Select command for the dataadapter return all rows? What the exception thrown, if any?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Shouldn't you be getting an Insert Command instead?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Yes I probably should... could you give me some proper code on how to impliment the insertcommand stuff?

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Adding A New Row to my DataSet :(

    VB Code:
    1. Dim NewRow As DataRow = DS.Tables("Users").NewRow
    2.         NewRow!UserName = txtUserName.Text
    3.         NewRow!Password = txtPassword.Text
    4.         NewRow!Type = "User"
    5.         DS.Tables("Users").Rows.Add(NewRow)
    6.  
    7.         Dim CB As New OleDbCommandBuilder(DA)
    8.         DA.UpdateCommand = CB.GetInsertCommand()
    9.         DA.Update(DS, "Users")

    Untested, of course.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    One more thing, please don't do this:

    NewRow!UserName = txtUserName.Text

    Instead, do this:

    NewRow("UserName") = txtUserName.Text

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    I'm getting an error here...

    VB Code:
    1. Users.Conn.Open()
    2.         Dim NewRow As DataRow = Users.DS.Tables("Users").NewRow
    3.         NewRow("UserName") = txtUserName.Text
    4.         NewRow("Password") = txtPassword.Text
    5.         NewRow("Type") = "User"
    6.         Users.DS.Tables("Users").Rows.Add(NewRow)
    7.  
    8.         Dim CB As New OleDbCommandBuilder(Users.DA)
    9.         Users.DA.UpdateCommand = CB.GetInsertCommand
    10. [b]        Users.DA.Update(Users.DS, "Users")[/b]
    11.         Users.Conn.Close()
    12.         MsgBox("Added New User!", MsgBoxStyle.OKOnly, "Success")
    13.         Me.DialogResult = DialogResult.OK

    An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

    not telling me anymore than that
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  8. #8
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494
    You can get a bit more info on the error if you catch the exception in a Try....Catch... block.

    Something like

    VB Code:
    1. Try
    2.         Users.DA.UpdateCommand = CB.GetInsertCommand
    3.         Users.DA.Update(Users.DS, "Users")
    4. Catch ex as Exception
    5.         Messagebox.Show (ex.ToString)
    6. End Try

    Try that and then post the full exception message and then maybe we can help some more?
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width