Solved

creating datacolumns and rows programmatically

Posted on 2006-07-10
12
344 Views
Last Modified: 2010-04-23
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
Comment
Question by:lytung818
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17078252
1) Where is Table1 defined and instantiated?

2) What exceptions are you getting?

Bob
0
 

Author Comment

by:lytung818
ID: 17078288
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17078372
My guess is 'Object variable or with block not set'.

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

Bob
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17078910
Bob...

Typo. Tsk, tsk.

Jeff
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17079649
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17080644
Ahem, the constructor for DataTable takes as an argument the table name, not column name.

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

Bob
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17080658
Bob,

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

Jeff
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17080791
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
 

Author Comment

by:lytung818
ID: 17083788
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
 
LVL 34

Expert Comment

by:Sancler
ID: 17084310
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
 

Author Comment

by:lytung818
ID: 17085696
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
 
LVL 34

Accepted Solution

by:
Sancler earned 250 total points
ID: 17086223
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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