Solved

creating datacolumns and rows programmatically

Posted on 2006-07-10
12
342 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
 
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
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.

 
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

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

Suggested Solutions

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

17 Experts available now in Live!

Get 1:1 Help Now