Results 1 to 6 of 6

Thread: DataTableAdapters add multiple rows help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    DataTableAdapters add multiple rows help

    Hi,

    I am very new to vb net and to database programming. I have done some VB6.

    I have been going round in circles for last 2 days, I have read MSN and googled all over place about the issue but to be honest it is not making much sense.

    What I am trying to do should be easy but I cant get any code to work.

    my thinking is build array then write this to database.

    I hav a form which ask several questions, this information is submitted to the server then repeats until logout

    I want to build up a table with rows after a user clicks submit button then say after 20 new rows added or a time period passes then this data is to be added to the server sqldatabase using the dataadapter update method, reason is to reduces hits on server

    I am using vb net 2010

    i have a dataset named dataset1.xsd

    dataadpater name = main_Testadapter
    datatable = main_Test
    columns = ID (auto unique), FirstName, Surname, Ref etc


    I am currently adding 1 row at a time, which works but there are many users and this could cause a hit on the server in future and I know it can be done but I cant get any where close to making it work, I know meant to use rows but Im stuck

    this is working for adding row when submit
    Me.Tbl_Main_TestAdapter.AddNew(FirstName, Surname, Ref, etc)


    any help appreciated.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: DataTableAdapters add multiple rows help

    don't add to the adaptor... add to the datatable...

    you should be adding rows to the Datatable...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    Re: DataTableAdapters add multiple rows help

    Quote Originally Posted by techgnome View Post
    don't add to the adaptor... add to the datatable...

    you should be adding rows to the Datatable...

    -tg
    I dont know the correct syntax to do that, I am very new with this stuff.

    Also this is auto generated and runs on form load, Me.Main_TestAdapter.Fill(Me.DataSet1.Main_Test)

    Am i correct in thinking this loads all the rows into memory, so if table had 10,000 this would be loaded into memory.

    is this required if all I wont to do is add new rows

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    Re: DataTableAdapters add multiple rows help

    I looked at this
    http://stackoverflow.com/questions/4...s-to-a-dataset

    I have tried this
    Dim Row As DataRow = DataSet1.Tables("Main_Test").NewRow()

    For i = 0 To 9
    Row("Department") = "Depart " & i.ToString
    Row("VerintID") = "123456" & i.ToString
    DataSet1.Tables("Main_Test").Rows.Add(Row)
    Next
    Main_TestAdapter.Update(DataSet1.Main_Test)

    but I get an error : this row already belongs to this table.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: DataTableAdapters add multiple rows help

    You're pretty close. The problem is that you create ONE new row in that first line, then fill it 10 times in the loop, and try to add it 10 times in the loop. That's a mistake, because it's the same row that you are filling over and over and over.

    Just move that first line (where you create the datarow) into the loop as the first line of the loop, and all should be good. The thing to remember is that you only create a new datarow when you call that New. Since you do that only once, you get only one new row. You need to be doing that each time through the loop so that you create 10 new rows.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    Re: DataTableAdapters add multiple rows help

    Thanks that's helped a lot

    I have done this now which is working and follows the type dataset method

    'typed dataset method
    For i = 0 To 9
    Dim Row As DataSet1.Main_TestRow
    Row = DataSet1.Main_Test.NewMain_TestRow

    Row.Department = "Depart " & i.ToString
    Row.VerintID = "123456" & i.ToString
    DataSet1.Main_Test.Rows.Add(Row)
    Next
    Main_TestAdapter.Update(DataSet1.Main_Test)

    and this will do same untyped method

    ' treat as untyped dataset method

    For i = 0 To 9
    Dim Row As DataRow = DataSet1.Tables("Main_Test").NewRow()

    Row("Department") = "Depart " & i.ToString
    Row("VerintID") = "123456" & i.ToString
    DataSet1.Tables("Main_Test").Rows.Add(Row)
    Next
    Main_TestAdapter.Update(DataSet1.Main_Test)

    Please correct me if any of this is wrong
    Last edited by jpskiller; Oct 22nd, 2016 at 10:48 AM.

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