Solved

Need help with code to insert rows into a dataset.

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

Author Comment

by:Sheryl Landis
ID: 40502863
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now