p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Basics (http://p2p.wrox.com/forumdisplay.php?f=60)
-   -   Creating Tables Programmatically (http://p2p.wrox.com/showthread.php?t=5229)

stu9820 October 21st, 2003 09:19 AM

Creating Tables Programmatically
 
Any good tutorials on how to create tables programmatically?


sal October 21st, 2003 11:19 AM

What is your database back end?

Sal

stu9820 October 21st, 2003 12:02 PM

It can be either MSDE or Access. I am familiar with both.


katsarosj October 21st, 2003 01:27 PM

'Make sure you import namespace...
Imports System.Web.UI.WebControls
'If you don't you will have to add "System.Web.UI.WebControls" to the beginning of each (table, tablerow, tablecell)

'**********************
Dim tblTable as Table
Dim tblRow as TableRow
Dim tblCell as TableCell

tblTable = New Table
tblRow = New TableRow
tblCell = New TableCell

tblCell.Text = "This text would appear in the first column."
tblRow.Cells.Add(tblCell)

tblCell = New TableCell
tblCell.Text = "This text would appear in the second column."
tblRow.Cells.Add(tblCell)

tblTable.Rows.Add(tblRow)

frmFormName.Controls.Add(tblTable)

'**********************
Hope this helps.

J

katsarosj October 21st, 2003 01:45 PM

Sorry,

Maybe I misunderstood...

If you want to create database tables in Access, try something like:

CREATE TABLE tblname
([Column1 Name] datatype, Column2Name datatype, Column3Name datatype);

To add a primary key:

CREATE TABLE TABLENAME
([Column1 Name] integer CONSTRAINT colIdx PRIMARY KEY, Column2Name text(25), Column3Name text(100));
INSERT INTO TABLENAME VALUES(integer_value, text_value, text_value);

I think this is correct, but been a while since I created Access tables in code.

Hope this helps and is what you are looking for.

J

sal October 21st, 2003 01:53 PM

for MSDE look up Books on line and do it Through a command object in ADO,

for access you can also use ADOX, to find more info open Access help and go to
Microsoft Access Help
Microsoft ActiveX Data Objects
Microsoft ActiveX Data Objects
Microsoft ADOX Programmer's Reference
ADOX API Reference

This has detailed info on this. To use it you have to r eference the ADOX library.

Sal

stu9820 October 22nd, 2003 07:38 AM

katsarosj, your first response is what I'm looking for.

Thanks.



stu9820 October 22nd, 2003 08:46 AM

Is it faster to build a table like this:
**********************
Dim tblTable as Table
Dim tblRow as TableRow
Dim tblCell as TableCell

tblTable = New Table
tblRow = New TableRow
tblCell = New TableCell

tblCell.Text = "This text would appear in the first column."
tblRow.Cells.Add(tblCell)

tblCell = New TableCell
tblCell.Text = "This text would appear in the second column."
tblRow.Cells.Add(tblCell)

tblTable.Rows.Add(tblRow)

frmFormName.Controls.Add(tblTable)
**************************


as opposed to this:
*****************
Dim strResultsHolder As String
strResultsHolder = "<table width=80% border=0 cellpadding=2 cellspacing=0>"
strResultsHolder &= "<tr><th colspan=3>" & myNews & "</th></tr>"

Dim r1 As DataRow
For Each r1 In objDataSet.Tables("dtNews").Rows
strResultsHolder &= "<tr><td width=140>" & r1("the_news") & "</td> <td>" & r1("short_sum") & "</td>"
If r1("news_link") = "No Link" Then
        strResultsHolder &= "<td width=90>No Link</td></tr>"
Else
        strResultsHolder &= "<td width=90><a href=" & r1("news_link") & " target=_blank>Get the story</a></td></tr>"
End If
Next

strResultsHolder &= "</table>"
display.InnerHtml = strResultsHolder

katsarosj October 22nd, 2003 01:41 PM

It is really up to you and what you are more comfortable with. I think the speed difference will be negligible. If you are using a code-behind file, then it gets compiled anyway which will speed it up.

The advantage to doing it the first way is that you can access the table/rows/columns methods and properties programmatically. Otherwise, I would just add the code as HTML(the portions where your are writing the table as strings) to the .aspx page (not the code behind) because it is less for the server to process.

Remember also that one of the great things about .net is the ability to "act" more like a windows form than classic asp.

So, if you want to create them programmatically, I would use the way that I presented to you. Hope this helps.

J

Quote:

quote:Originally posted by stu9820
 Is it faster to build a table like this:
**********************
Dim tblTable as Table
Dim tblRow as TableRow
Dim tblCell as TableCell

tblTable = New Table
tblRow = New TableRow
tblCell = New TableCell

tblCell.Text = "This text would appear in the first column."
tblRow.Cells.Add(tblCell)

tblCell = New TableCell
tblCell.Text = "This text would appear in the second column."
tblRow.Cells.Add(tblCell)

tblTable.Rows.Add(tblRow)

frmFormName.Controls.Add(tblTable)
**************************


as opposed to this:
*****************
Dim strResultsHolder As String
strResultsHolder = "<table width=80% border=0 cellpadding=2 cellspacing=0>"
strResultsHolder &= "<tr><th colspan=3>" & myNews & "</th></tr>"

Dim r1 As DataRow
For Each r1 In objDataSet.Tables("dtNews").Rows
strResultsHolder &= "<tr><td width=140>" & r1("the_news") & "</td> <td>" & r1("short_sum") & "</td>"
If r1("news_link") = "No Link" Then
        strResultsHolder &= "<td width=90>No Link</td></tr>"
Else
        strResultsHolder &= "<td width=90><a href=" & r1("news_link") & " target=_blank>Get the story</a></td></tr>"
End If
Next

strResultsHolder &= "</table>"
display.InnerHtml = strResultsHolder



stu9820 October 22nd, 2003 02:18 PM

Once I have this:

objCell = New HtmlTableCell()
objCell.InnerHtml = "Time"
objCell.BgColor = "#C0C0C0"
objCell.Align = "center"
objRow.Cells.Add(objCell)

is there any way to have the row highlight when you move your mouse over it?



All times are GMT -4. The time now is 05:21 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.