Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Need help with code to insert rows into a dataset.

Posted on 2014-12-16
8
82 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 63

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 63

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 63

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 63

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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