?
Solved

Need help with code to insert rows into a dataset.

Posted on 2014-12-16
8
Medium Priority
?
85 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
[X]
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
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 2000 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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