Solved

creating datacolumns and rows programmatically

Posted on 2006-07-10
12
340 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
Comment Utility
1) Where is Table1 defined and instantiated?

2) What exceptions are you getting?

Bob
0
 

Author Comment

by:lytung818
Comment Utility
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
Comment Utility
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
Comment Utility
Bob...

Typo. Tsk, tsk.

Jeff
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
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
Comment Utility
Ahem, the constructor for DataTable takes as an argument the table name, not column name.

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

Bob
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
Bob,

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

Jeff
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

11 Experts available now in Live!

Get 1:1 Help Now