Solved

creating datacolumns and rows programmatically

Posted on 2006-07-10
12
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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