Create Dataset and populate it with data contained in 6 textboxes   How do I do it?

Posted on 2007-04-09
Last Modified: 2010-08-05
How do I create and populate a dataset from Textboxes ...

I have lets say 6 textboxes containing data  inputed by user

On a button click I would like to create the Dataset and populate it with the data contained in these textboxes

How do I do that?

Question by:vbMarkO
  • 2

Expert Comment

ID: 18877909
Is the data from the 6 textboxes all for the same field or for 6 seperate fields?

Accepted Solution

CBeach1980 earned 500 total points
ID: 18877961
If they are for seperate fields you would do something like this:
        Dim ds As New DataSet()

        Dim newTable As New DataTable()

        Dim newRow As DataRow
        newRow = newTable.NewRow()

        newRow("Field1") = textbox1.Text
        newRow("Field2") = textbox2.Text
        newRow("Field3") = textbox3.Text
        newRow("Field4") = textbox4.Text
        newRow("Field5") = textbox5.Text
        newRow("Field6") = textbox6.Text


Otherwise if you want them all for the same field you would do something like this:
        Dim ds As New DataSet()

        Dim newTable As New DataTable()

        Dim newRow As DataRow
        newRow = newTable.NewRow()
        newRow("Field1") = textbox1.Text
        newRow = newTable.NewRow()
        newRow("Field2") = textbox2.Text
        newRow = newTable.NewRow()
        newRow("Field3") = textbox3.Text
        newRow = newTable.NewRow()
        newRow("Field4") = textbox4.Text
        newRow = newTable.NewRow()
        newRow("Field5") = textbox5.Text
        newRow = newTable.NewRow()
        newRow("Field6") = textbox6.Text

Let me know if that is what you're looking for.
LVL 13

Expert Comment

ID: 18879822
Here is a simple form with Data Binding

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.DataSet1 = New System.Data.DataSet
        Me.DataTable1 = New System.Data.DataTable
        Me.DataColumn1 = New System.Data.DataColumn
        Me.DataColumn2 = New System.Data.DataColumn
        Me.DataColumn3 = New System.Data.DataColumn
        Me.DataColumn4 = New System.Data.DataColumn
        Me.DataColumn5 = New System.Data.DataColumn
        Me.DataColumn6 = New System.Data.DataColumn
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.TextBox4 = New System.Windows.Forms.TextBox
        Me.TextBox5 = New System.Windows.Forms.TextBox
        Me.TextBox6 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.BindingSource1 = New System.Windows.Forms.BindingSource(Me.components)
        Me.DataGridView1 = New System.Windows.Forms.DataGridView
        Me.Column1DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Column2DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Column3DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Column4DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Column5DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Column6DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataTable1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.BindingSource1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.DataSet1.DataSetName = "NewDataSet"
        Me.DataSet1.Tables.AddRange(New System.Data.DataTable() {Me.DataTable1})
        Me.DataTable1.Columns.AddRange(New System.Data.DataColumn() {Me.DataColumn1, Me.DataColumn2, Me.DataColumn3, Me.DataColumn4, Me.DataColumn5, Me.DataColumn6})
        Me.DataTable1.TableName = "Table1"
        Me.DataColumn1.ColumnName = "Column1"
        Me.DataColumn2.ColumnName = "Column2"
        Me.DataColumn3.ColumnName = "Column3"
        Me.DataColumn4.ColumnName = "Column4"
        Me.DataColumn5.ColumnName = "Column5"
        Me.DataColumn6.ColumnName = "Column6"
        Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Column1", True))
        Me.TextBox1.Location = New System.Drawing.Point(126, 45)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(235, 22)
        Me.TextBox1.TabIndex = 0
        Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Column2", True))
        Me.TextBox2.Location = New System.Drawing.Point(126, 73)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(235, 22)
        Me.TextBox2.TabIndex = 1
        Me.TextBox3.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Column3", True))
        Me.TextBox3.Location = New System.Drawing.Point(126, 101)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(235, 22)
        Me.TextBox3.TabIndex = 2
        Me.TextBox4.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Column4", True))
        Me.TextBox4.Location = New System.Drawing.Point(126, 129)
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(235, 22)
        Me.TextBox4.TabIndex = 3
        Me.TextBox5.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Column5", True))
        Me.TextBox5.Location = New System.Drawing.Point(126, 157)
        Me.TextBox5.Name = "TextBox5"
        Me.TextBox5.Size = New System.Drawing.Size(235, 22)
        Me.TextBox5.TabIndex = 4
        Me.TextBox6.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Column6", True))
        Me.TextBox6.Location = New System.Drawing.Point(126, 185)
        Me.TextBox6.Name = "TextBox6"
        Me.TextBox6.Size = New System.Drawing.Size(235, 22)
        Me.TextBox6.TabIndex = 5
        Me.Button1.Location = New System.Drawing.Point(126, 229)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(128, 40)
        Me.Button1.TabIndex = 6
        Me.Button1.Text = "Add"
        Me.Button1.UseVisualStyleBackColor = True
        Me.Button2.Location = New System.Drawing.Point(271, 229)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(128, 40)
        Me.Button2.TabIndex = 7
        Me.Button2.Text = "Apply"
        Me.Button2.UseVisualStyleBackColor = True
        Me.BindingSource1.DataMember = "Table1"
        Me.BindingSource1.DataSource = Me.DataSet1
        Me.DataGridView1.AutoGenerateColumns = False
        Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1DataGridViewTextBoxColumn, Me.Column2DataGridViewTextBoxColumn, Me.Column3DataGridViewTextBoxColumn, Me.Column4DataGridViewTextBoxColumn, Me.Column5DataGridViewTextBoxColumn, Me.Column6DataGridViewTextBoxColumn})
        Me.DataGridView1.DataSource = Me.BindingSource1
        Me.DataGridView1.Location = New System.Drawing.Point(21, 275)
        Me.DataGridView1.Name = "DataGridView1"
        Me.DataGridView1.RowTemplate.Height = 24
        Me.DataGridView1.Size = New System.Drawing.Size(593, 113)
        Me.DataGridView1.TabIndex = 8
        Me.Column1DataGridViewTextBoxColumn.DataPropertyName = "Column1"
        Me.Column1DataGridViewTextBoxColumn.HeaderText = "Column1"
        Me.Column1DataGridViewTextBoxColumn.Name = "Column1DataGridViewTextBoxColumn"
        Me.Column2DataGridViewTextBoxColumn.DataPropertyName = "Column2"
        Me.Column2DataGridViewTextBoxColumn.HeaderText = "Column2"
        Me.Column2DataGridViewTextBoxColumn.Name = "Column2DataGridViewTextBoxColumn"
        Me.Column3DataGridViewTextBoxColumn.DataPropertyName = "Column3"
        Me.Column3DataGridViewTextBoxColumn.HeaderText = "Column3"
        Me.Column3DataGridViewTextBoxColumn.Name = "Column3DataGridViewTextBoxColumn"
        Me.Column4DataGridViewTextBoxColumn.DataPropertyName = "Column4"
        Me.Column4DataGridViewTextBoxColumn.HeaderText = "Column4"
        Me.Column4DataGridViewTextBoxColumn.Name = "Column4DataGridViewTextBoxColumn"
        Me.Column5DataGridViewTextBoxColumn.DataPropertyName = "Column5"
        Me.Column5DataGridViewTextBoxColumn.HeaderText = "Column5"
        Me.Column5DataGridViewTextBoxColumn.Name = "Column5DataGridViewTextBoxColumn"
        Me.Column6DataGridViewTextBoxColumn.DataPropertyName = "Column6"
        Me.Column6DataGridViewTextBoxColumn.HeaderText = "Column6"
        Me.Column6DataGridViewTextBoxColumn.Name = "Column6DataGridViewTextBoxColumn"
        Me.Button3.Location = New System.Drawing.Point(415, 83)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(128, 40)
        Me.Button3.TabIndex = 6
        Me.Button3.Text = "Move Up"
        Me.Button3.UseVisualStyleBackColor = True
        Me.Button4.Location = New System.Drawing.Point(415, 129)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(128, 40)
        Me.Button4.TabIndex = 7
        Me.Button4.Text = "Move Down"
        Me.Button4.UseVisualStyleBackColor = True
        Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(626, 388)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataTable1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.BindingSource1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub
    Friend WithEvents DataSet1 As System.Data.DataSet
    Friend WithEvents DataTable1 As System.Data.DataTable
    Friend WithEvents DataColumn1 As System.Data.DataColumn
    Friend WithEvents DataColumn2 As System.Data.DataColumn
    Friend WithEvents DataColumn3 As System.Data.DataColumn
    Friend WithEvents DataColumn4 As System.Data.DataColumn
    Friend WithEvents DataColumn5 As System.Data.DataColumn
    Friend WithEvents DataColumn6 As System.Data.DataColumn
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox6 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents BindingSource1 As System.Windows.Forms.BindingSource
    Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
    Friend WithEvents Column1DataGridViewTextBoxColumn As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Column2DataGridViewTextBoxColumn As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Column3DataGridViewTextBoxColumn As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Column4DataGridViewTextBoxColumn As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Column5DataGridViewTextBoxColumn As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Column6DataGridViewTextBoxColumn As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button

End Class
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    End Sub
End Class

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 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

18 Experts available now in Live!

Get 1:1 Help Now