• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1290
  • Last Modified:

Create dataset programatically in vb.net 2005

Hi,

I'm trying to find out how to create a dataset and table programatically.

Basically I need a table that will store textbox1.text in column 1 and textbox2.text in column 2

How do I do this please?

Many thanks
Lee
0
ljhodgett
Asked:
ljhodgett
  • 2
1 Solution
 
ToFroCommented:
Try this:

        Dim ds As New DataSet
        Dim dt As New DataTable("table1")

        dt.Columns.Add("Column1", GetType(String))
        dt.Columns.Add("Column2", GetType(String))

        Dim row As DataRow = dt.NewRow
        row("Column1") = TextBox1.Text
        row("Column2") = TextBox2.Text
        dt.Rows.Add(row)
        ds.Tables.Add(dt)
0
 
ToFroCommented:
Hi there again, I'm assuming you are familar with events. You could use the code like this:

... declare the DataSet globally

    Private ds As DataSet


... then create the DataTable and columns on page load:

        ds = New DataSet
        Dim dt As DataTable = New DataTable("table1")
        dt.Columns.Add("Column1", GetType(String))
        dt.Columns.Add("Column2", GetType(String))
        ds.Tables.Add(dt)


... every time you want to add a row, do this(e.g. behind a button event):

        Dim row As DataRow = ds.Tables("table1").NewRow
        row("Column1") = TextBox1.Text
        row("Column2") = TextBox2.Text
        ds.Tables("table1").Rows.Add(row)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now