-
Oct 21st, 2016, 02:17 PM
#1
Thread Starter
Lively Member
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.
-
Oct 21st, 2016, 02:21 PM
#2
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
-
Oct 21st, 2016, 02:30 PM
#3
Thread Starter
Lively Member
Re: DataTableAdapters add multiple rows help
Originally Posted by techgnome
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
-
Oct 21st, 2016, 03:28 PM
#4
Thread Starter
Lively Member
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.
-
Oct 21st, 2016, 04:35 PM
#5
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
-
Oct 22nd, 2016, 09:42 AM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|