Problem Generating Table

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Hello everyone,

I am trying to generate a table on my form with rows/cells. I can do this fine with response.write; however, I am trying to pull away from that. What I am using is this:

VB.NET:
[color=#0000ff]Dim[/color] tr [color=#0000ff]As[/color] TableRow
[color=#0000ff]Dim[/color] td [color=#0000ff]As[/color] TableCell
[color=#0000ff]Dim[/color] tbl [color=#0000ff]As[/color] Table
[color=#0000ff]Dim[/color] ph[color=#0000ff]As[/color] PlaceHolder
 
[size=3][size=2]tr = [color=#0000ff]New[/color] TableRow[/size]
td = [color=#0000ff]New[/color] TableCell
td.Controls.Add([color=#0000ff]New[/color] LiteralControl("Hello World"))
td.Attributes.Add("style", "width:4px; color:steelblue; font-size:2;")
tr.Cells.Add(td)
tbl.Rows.Add(tr)
[size=3][size=2]ph.Controls.Add(tbl)[/size]
[/size]But this doesn't work. I get the following at tbl.Rows.Add(tr) What am I doing wrong?

Object reference not set to an instance of an object.

Also - if I set tbl = New Table, I get the same error at phfiles.controls.add(tbl)

[/size]
 
Last edited:

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
sorry - I am pretty new at this stuff - I am not sure what you mean. Can you give me an example or something.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
One of the most fundamental rules of Object Oriented Programming is that you must instantiate an object before you can use it. The following line:
VB.NET:
[color=Blue]Dim[/color] tr [color=Blue]As[/color] TableRow
declares the variable tr as being of type TableRow. At this point, it is not actually a TableRow but only a place that a TableRow can be stored. The following line:
VB.NET:
tr = [color=Blue]New[/color] TableRow
creates an instance of, or instantiates, the TableRow class and assigns it to tr. You can declare and instantiate a variable in one line like so:
VB.NET:
[color=Blue]Dim[/color] tr [color=Blue]As New[/color] TableRow
In your code, you have created a New TableRow and a New TableCell, but not a New Table or PlaceHolder. This means that you cannot legally use the tbl or ph variables as they do not refer to actual objects. You must instantiate them first:
VB.NET:
tbl = [color=Blue]New[/color] Table
tbl.Rows.Add(tr)
ph = [color=Blue]New[/color] PlaceHolder
ph.Controls.Add(tbl)
 

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Alright - that made sense about what you said - I have fixed that now. I just can't seem to get anything to display on my screen - meaning, it doesn't appear to be generating my table. I have the following...btw, this is in the Page Load event...
VB.NET:
[color=blue]Dim[/color] tbl [color=blue]As[/color] [color=blue]New [/color]Table
[color=blue]Dim[/color] tr [color=blue]As[/color] [color=blue]New[/color] TableRow
[color=blue]Dim[/color] td [color=blue]As[/color] [color=blue]New[/color] TableCell
[color=blue]Dim[/color] ph [color=blue]As[/color] [color=blue]New[/color] PlaceHolder
 
td.Controls.Add([color=#0000ff]New[/color] LiteralControl("Hello World"))
[size=3][size=2]td.Attributes.Add("style", "width:4px; color:steelblue; font-size:2;")[/size]
[size=2]tr.Cells.Add(td)
tbl.Rows.Add(tr)
[/size][size=3][size=2]ph.Controls.Add(tbl)[/size][/size][/size]
...and I no longer get an error - but I just get a blank page. What am I missing here?
 
Last edited:

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Ok, I have progressed a little bit...

If I first create my Table on the Webform - then go into my Page-Load event and code:
VB.NET:
[color=#0000ff]Dim[/color] tr [color=#0000ff]As[/color] [color=#0000ff]New[/color] TableRow
[color=#0000ff]Dim[/color] td [color=#0000ff]As[/color] [color=#0000ff]New[/color] TableCell
td.Controls.Add([color=#0000ff]New[/color] LiteralControl("Hello World"))
[color=#008000]'td.Attributes.Add("style", "width:4px; color:steelblue; font-size:2;")[/color]
tr.Cells.Add(td)
[size=3][size=2]Table1.Rows.Add(tr)[/size][/size]
[size=3][size=2]
[/size][/size]
This will add the text "Hello World" in the cell. If I uncomment the td.attributes.add - then all I get is a blank page. What I was trying to do is, without first creating a table, dynamically generate a table on the page load event and set the proper attributes for the cells - I just can't seem to figure it out.
 
Top Bottom