Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with code to insert rows into a dataset.

Posted on 2014-12-16
8
Medium Priority
?
86 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 64

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 64

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 64

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 64

Expert Comment

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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

610 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