Solved

Need help with code to insert rows into a dataset.

Posted on 2014-12-16
8
81 Views
Last Modified: 2014-12-16
I normally add data using classes, but I am building a prototype and using data sources in the project.  The data source is called 'ProgramDataSet' which contains all the tables for the application.  In this example I am trying to add a new row for each cutting from a plant, with the number of plants to start stored in the variable 'starts'.

This should be simple, but none of the code samples I found showed how to do this in .Net 4.5 and VB 2012.

The code obviously doesn't work, but hopefully you will understand what I'm trying to do and present some sample code to do this correctly.  Thanks!

Try
            Dim starts As Integer = CInt(Me.NumberOfStartsTextBox.Text)
            Dim r As DataRow = ProgramDataSet.PlantsRow
            For x As Integer = 1 To starts
                r.BeginEdit()
                r.StrainCode = Me.StrainCodeComboBox.Text
                r.StartDate = Me.StartDateDateTimePicker.Value
                r.Description = Me.DescriptionTextBox.Text
                r.EndEdit()
                r.AcceptChanges()

Open in new window

0
Comment
Question by:Sheryl Landis
  • 4
  • 4
8 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40502703
Hi Sheryl Landis;

I think what you want is something like this.

Dim starts As Integer = CInt(Me.NumberOfStartsTextBox.Text)

For x As Integer = 1 To starts
    Dim r = ProgramDataSet.Tables("Table Name Here").NewRow()
    
    r("StrainCode") = Me.StrainCodeComboBox.Text
    r("StartDate") = Me.StartDateDateTimePicker.Value
    r("Description") = Me.DescriptionTextBox.Text
    ProgramDataSet.Tables("Table Name Here").Rows.Add(r)

End For

Open in new window

0
 

Author Comment

by:Sheryl Landis
ID: 40502753
I tried this, and the problem is that the intellisense doesn't give me an option for .Tables in ProgramDataSet.  It has the following properties for EACH table, where you would substitute 'Plants' with 'Groups' for example:

PlantsDataTable
PlantsRow
PlantsRowChangeEvent
PlantsRowChangeEventHandler

GroupsDataTable
GroupsRow
etc....

I am Importing System.Data and System.Data.SqlClient.  Am I missing something?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40502819
Hi Sheryl ;

It sounds like it is a Typed DataSet created by a DataSet wizard. Can you please post the class  ProgramDataSet. I have not worked a lot with Typed DataSet's and need to see what is in it.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Sheryl Landis
ID: 40502863
0
 

Author Comment

by:Sheryl Landis
ID: 40502899
I'm beginning to think that what I'm trying to do is not possible, or it is not worth the trouble to figure it out.  I've always coded using table adapters and readers, but am trying to go along with a sample in a book.  If you do find a way to make it work though, that would be wonderful!
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40503364
Hi Sheryl;

I think this should do what you are trying to do.  
             
    
Try            
    Dim starts As Integer = CInt(Me.NumberOfStartsTextBox.Text)    
    '' Add the TableAdapter
    Dim plantsTAdapter As PlantsTableAdapter = New PlantsTableAdapter()
    '' Add the DataTable
    Dim plantsTable As PlantsDataTable = New  PlantsDataTable()
    
    For x As Integer = 1 To starts
        '' Get a row from the TableAdapter for the Plants table
        Dim r As DataRow = plantsTAdapter.NewPlantsRow()
        '' Fill the row
        r.StrainCode = Me.StrainCodeComboBox.Text
        r.StartDate = Me.StartDateDateTimePicker.Value
        r.Description = Me.DescriptionTextBox.Text
        '' ....
        
        '' Add the row to the table
        plantsTable.AddPlantsRow(r)
    End For
Catch ex As Exception
    '' .....
End Try

Open in new window

0
 

Author Closing Comment

by:Sheryl Landis
ID: 40503512
You did it, cool!  Thanks for helping again, Fernado!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40503520
Not a problem Sheryl; glad to help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now