table adapter help

  • 11 years ago

    Hi all,  I am getting really stressed and I was hoping someone could help me out.  I am trying to add some rows to a table using table adapters in VB.net 2005 express and sql server express.  I have read the stuff on the MSDN site and it still does not work does anybody have any ideas.

     

    Thanks,

  • 11 years ago
    Create an instance of your DataTable type, add rows to it, call the Update method of the TableAdapter and pass the DataTable as a parameter, e.g.
    Dim adapter As New MyDataSetTableAdapters.MyTableTableAdapter
    Dim table As New MyDataSet.MyTable
    Dim row As MyDataSet.MyTableRow = table.NewMyTableRow
    
    row.ID = 101
    row.Name = "My Name"
    table.AddMyTableRow(row)
    adapter.Update(table)
  • 11 years ago
    Sorry if I am being confusing.  I have been working on this all day and am stressed out.  I just want to be done with this.

    I have a dataset called mydataset and it has a table called folder.  The two fields in the tabel are FolderID and FolderName.  FolderID is the key and it is on autonumber.  I want to be able to enter a folder name in a text box, click an OK button and then have a new table row with this new table name added to the database.

    Thanks,





  • 11 years ago
    Re-read your post and realised you meant add rows to a database tabel as opposed to add rows to a DataTable from the database.  Edited my post accordingly.  You really should read some basic ADO.NET tutorials so you understand the principles.

  • 11 years ago

    Thanks for your reply.I have tried things similar to that.  The problem is with the second line:

    Dim table As New MyDataSet.MyTable

    When I Type the period after MyDataSet.  It does not give me the option for MyTable.  It lets me choose from objects like FolderRow, FolderRowChangeEvent, and FolderDataTable.

    I am assuming that maybe it is FolderDataTable, so my code looks like this:

     
            Dim adapter As New DataSet1TableAdapters.FolderTableAdapter
             Dim table As New DataSet1
            Dim row As DataSet1.FolderRow

            row = table.NewFolderRow
             row.FolderName = "fred"
             table.AddFolderRow(row)
             adapter.Update(table)
    However it still does not work.  I appreciate your help on this.  

    I am new to VB.net and this is the last part of this application
    I need to get done.  The rest of the project I get to write in PHP and that will be a breeze.


  • 11 years ago
    What I am doing is tying that code to a button and When I click the button I would think it should add a folder named "fred" to the datatable.  However when I close the table and look at the data in my folder table nothing has changed.





  • 11 years ago
    The Update method is a function and it returns the number of affected rows.  That's the way to test whether it's working or not.  If Update returns a number greater than zero then it's working.  Wrap call to Update in a MessageBox.Show call to see what number it's returning.  If you're exiting the application and then running it again and the changes are not there then chances are you're just overwriting the database each time you compile.  The default for an MDF file is to "Copy always".  To avoid overwriting every time you can change the "Copy to Output Directory" property of your MDF file to "Copy if newer" or "Do not copy", which ever is most appropriate.

  • 11 years ago
    Thanks again for your help.  I created a message box like you said and it keeps coming up that I have updated 1 row, however no change is reflected in the database.  I also change the copy always settings on the mdf file like you said.  I also tried different settings with the dataset and still no luck.

    Maybe it is just some weird setting, but I do not see what that could be.







  • 11 years ago
    If Update is returning 1 then you've updated a row.  That's a fact.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow