TimmyHoHoo TimmyHoHoo - 1 year ago 67
Vb.net Question

Can you populate a Dataset with User Input?

I need to store user input (entered from the console) to a Dataset in Visual Basic. Is this possible? I can't seem to find any information online about doing so. If anyone can point me in the right direction, that would be awesome!

EDIT: The columns in my table are FirstName, LastName, State, etc. in a table named Person.
The start of the application for the console: "Enter FirstName: ", then "EnterLastName: " then etc.

The values for the user inputs are stored in there specific variables eg. firstName, lastName, so I want to know how I can put those variables in a dataset.

Thank you.

Answer Source

Here is a short and basic example. Most likely it will not directly work for what you need as you will probably want to use a typed dataset among other things. Since your question does not specify a question regarding the code you may have already tried, this all I am going to provide.

Option Strict On

Module Module1

    Sub Main()    
        Dim ds As DataSet = CreateNewDataSet()
        Dim entries As New List(Of String())
        For i = 1 To 3
            Console.WriteLine($"Enter first column value for row {i.ToString}:")
            Dim input1 As String = Console.ReadLine()
            Console.WriteLine($"Enter second column value for row {i.ToString}:")
            Dim input2 As String = Console.ReadLine()
            entries.Add({input1, input2})
        entries.ForEach(Sub(x) ds.Tables(0).Rows.Add(x))    
    End Sub

    Private Function CreateNewDataSet() As DataSet
        Dim ds As New DataSet()
        Dim dt As New DataTable()
        Dim col1 As New DataColumn("Column1")
        Dim col2 As New DataColumn("Column2")
        dt.Columns.AddRange({col1, col2})
        Return ds
    End Function
End Module