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

creating datacolumns and rows programmatically

Hi experts,

i am trying to write this program that can manually add columns and rows dynamically but i keep getting errors..what is wrongwith this code?

 Private Sub InsertDataset(ByVal colName As String, ByVal datafield As String)


        'creating a table named Customers
        Dim Row1 As DataRow
        'declaring three rows for the table
        Try
            Dim Name As DataColumn = New DataColumn(colName)
            'declaring a column named Name
            Name.DataType = System.Type.GetType("System.String")
            'setting the datatype for the column
            Table1.Columns.Add(Name)
            Row1 = Table1.NewRow()
            'declaring a new row
            Row1.Item("Name") = datafield
            Table1.Rows.Add(Row1)
            'adding the completed row to the table

        Catch
        End Try

    End Sub
0
lytung818
Asked:
lytung818
  • 4
  • 3
  • 3
  • +1
1 Solution
 
Bob LearnedCommented:
1) Where is Table1 defined and instantiated?

2) What exceptions are you getting?

Bob
0
 
lytung818Author Commented:
i wanted the table to be global so its right under the imports statements.
i am getting an error in this line,  Table1.Columns.Add(Name)

sorry not at work i can't remember the error message but it wasn't very descriptive
0
 
Bob LearnedCommented:
My guess is 'Object variable or with block not set'.

Table1 = New DataTable("Name") ' <-- "Wew" replaced with "New"... Chaosian

Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jeff CertainCommented:
Bob...

Typo. Tsk, tsk.

Jeff
0
 
SanclerCommented:
1)  Nothing that would break, but (Bob's suggestion) it might be a bit confusing to call the Table "Name" as well as having a column named "Name" ...

2)  ... although - despite the comment

            'declaring a column named Name

- we don't actually know that it is called Name.

           Dim Name As DataColumn = New DataColumn(colName)

will give it the name that is in the variable colName which is not declared or given any value anywhere that I can see.  So ...

3)  ... this line

            Row1.Item("Name") = datafield

could (a) be trying to reference an item that doesn't exist and (b) is putting something in it which has not been declared or given any value anywhere that I can see.

Apart from that, after taking account of Bob's and Jeff's comments, it looks OK to me ;-)

Roger
0
 
Bob LearnedCommented:
Ahem, the constructor for DataTable takes as an argument the table name, not column name.

Thank you for the "Wew" correction, Jeff.  ;)

Bob
0
 
Jeff CertainCommented:
Bob,

Glad to help. After all, we are *technical* experts, not typing experts LOL

Jeff
0
 
SanclerCommented:
Bob

>>
Ahem, the constructor for DataTable takes as an argument the table name, not column name.
<<

Yes.  

So

Table1 = New DataTable("Name")

produces a Table named "Name", and if this comment in the original code is correct (but see my point (2) above)

            'declaring a column named Name

we also have a column named "Name".

As I said, it wouldn't break.  It's style, not substance.  But I for one would not immediately know which was being referred to when I saw "Name".

Or did I misunderstand that "Ahem"? ;-)

Roger
0
 
lytung818Author Commented:
okay the error i was getting was
Column 'Name' does not belong to table Order. I put "Name" and it worked. I was trying to put a variable instead of the " " and it doesn't work. Is there a way to do this? i have like 50 columns that i want to create, i dont think i want to keep doing

    Table1.Columns.Add("column1")
    Table1.Columns.Add("column2")
    Table1.Columns.Add("column3")

i was tryingto do

    Table1.Columns.Add(colName)
0
 
SanclerCommented:
Like this?

   For i As Integer = 1 To 50
      Dim dc As New DataColumn("column" & i.ToString.PadLeft(2, "0"))
      dc.DataType = GetType(String)
      Table1.Columns.Add(dc)
   Next

Roger
0
 
lytung818Author Commented:
Well i am getting confused. I want my dataset to look like this:

           column1 column2    column3 column4 column5..etc etc
Row     1            1/1/2006  test        test       test          


I am most likely only going to have one row but 50 columns.  I want to pass the column name to the function , and then also pass the value for the row. This funciton is not working right now:


Private Sub InsertDataset(ByVal datafield As String, ByVal colName as String)
     
        Try
                     
            Dim dc As New DataColumn(colName)
            dc.DataType = GetType(String)
            Table1.Columns.Add(dc)


            Dim Row1 As DataRow
            Row1 = Table1.NewRow()
            Row1.Item("Name") = datafield
            Table1.Rows.Add(Row1)
           
        Catch
        End Try

    End Sub

0
 
SanclerCommented:
The problem with that is - or may be - that your column is being given the name that you pass as the argument colName whereas you are referring to it in this line

            Row1.Item("Name") = datafield

specifically as "Name".  So, was the argument colName passed as "Name"?  If not, that would be a problem.  If it was, that should not be a problem.

I assume, following the earlier posts, that you have both declared and instantiated Table1?

Apart from that, it looks OK.  So far as it goes.  But it's a long way from creating fifty columns and filling them all with data - even on one row ;-)

Roger
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now