467,084 Members | 1,243 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,084 developers. It's quick & easy.

Adding Data To a Table That Isn't Part Of the Original SQL Query

I am fairly new to ASP.NET so I think I am missing something fundamental.
Anyway, quite often I am pulling data from a database, but then I need to
use that data to produce more data. A simple example would be: Let's say
Column1=StartDate and Column2=EndDate. In addition to displaying Column1 and
Column2, I need to do some calculations and display in as Column3.

The calculations are easy and can be done in the code-behind. How to display
it is another question.
However, they might not be math calculations. Take for example, a SQL query
that returns a list of servers and then for each server I use WMI to get
some additional information and then I need to display the servername (from
SQL) and the OS and Service Pack Version (from WMI) on a web page.

The way I have been handling it is creating a new table in memory and adding
the columns from the original SQL query and then doing the calculations and
adding that as a column. Then I'll use this new table to DataBind my
DataGrid or DataList.

Is this the best way to do it? I feel like this isn't the smartest way to
acomplish this.
Nov 18 '05 #1
  • viewed: 1670
Share:
3 Replies
Here is an example of adding a column to a datatable this is based on two
other columns

Dim myDataColumn As DataColumn= new datacolumn("ShipDisplay")
myDataColumn.datatype=System.Type.GetType("System. String")
myDataColumn.expression = ("Description + ' cost $' + Charge")
dt.columns.add(myDatacolumn)

Please note that the expression not an asp.net statement, it is like a
database statement. In the above example the expression is the data in the
Description column concatenated with the word 'cost $' and data in the
Charge column. So if I had and row item in my table with the Description
'Express' and the Charge column was "20" then the new column would display
'Express cost $20' for that data row. I don't think you can use variables
(except for constant strings) in the expression statement. You can use math
and other functions like you would in a database statement but it has to be
based on the columns of the data base.

Hope this helps.

"Robin Thomas" <me@winterminute.com.NOSPAM> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
I am fairly new to ASP.NET so I think I am missing something fundamental.
Anyway, quite often I am pulling data from a database, but then I need to
use that data to produce more data. A simple example would be: Let's say
Column1=StartDate and Column2=EndDate. In addition to displaying Column1 and Column2, I need to do some calculations and display in as Column3.

The calculations are easy and can be done in the code-behind. How to display it is another question.
However, they might not be math calculations. Take for example, a SQL query that returns a list of servers and then for each server I use WMI to get
some additional information and then I need to display the servername (from SQL) and the OS and Service Pack Version (from WMI) on a web page.

The way I have been handling it is creating a new table in memory and adding the columns from the original SQL query and then doing the calculations and adding that as a column. Then I'll use this new table to DataBind my
DataGrid or DataList.

Is this the best way to do it? I feel like this isn't the smartest way to
acomplish this.

Nov 18 '05 #2
Yes, this helps very much. What if I didn't want that column to be a
calculation based on other columns (as your example shows), but rather
additional information for that record.

Is there something other than "myDataColumn.expression=" that I could set
the value of the new column to a string value?
"vMike" <Mi****************@noYandZ.geZwaYrrenZ.com> wrote in message
news:bu**********@ngspool-d02.news.aol.com...
Here is an example of adding a column to a datatable this is based on two
other columns

Dim myDataColumn As DataColumn= new datacolumn("ShipDisplay")
myDataColumn.datatype=System.Type.GetType("System. String")
myDataColumn.expression = ("Description + ' cost $' + Charge")
dt.columns.add(myDatacolumn)

Please note that the expression not an asp.net statement, it is like a
database statement. In the above example the expression is the data in the
Description column concatenated with the word 'cost $' and data in the
Charge column. So if I had and row item in my table with the Description
'Express' and the Charge column was "20" then the new column would display
'Express cost $20' for that data row. I don't think you can use variables
(except for constant strings) in the expression statement. You can use math and other functions like you would in a database statement but it has to be based on the columns of the data base.

Hope this helps.

"Robin Thomas" <me@winterminute.com.NOSPAM> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple example would be: Let's say
Column1=StartDate and Column2=EndDate. In addition to displaying Column1

and
Column2, I need to do some calculations and display in as Column3.

The calculations are easy and can be done in the code-behind. How to

display
it is another question.
However, they might not be math calculations. Take for example, a SQL

query
that returns a list of servers and then for each server I use WMI to get
some additional information and then I need to display the servername

(from
SQL) and the OS and Service Pack Version (from WMI) on a web page.

The way I have been handling it is creating a new table in memory and

adding
the columns from the original SQL query and then doing the calculations

and
adding that as a column. Then I'll use this new table to DataBind my
DataGrid or DataList.

Is this the best way to do it? I feel like this isn't the smartest way to acomplish this.


Nov 18 '05 #3
Well you can set it to some contant value here. Otherwise you add the column
with no expression statement and then
you will have to iterate through the entire datatable and add a value to
each row using a for i = 0 to dt.rows.count - 1 ..
dt.rows(i)("yournewcolumnname") = i (or somevalue) ... next i . (this is
common for adding sequential row numbers to the datatable) This will have
to be done every time you create the datatable unless you are able to save
the table in a database or xml file.

"Robin Thomas" <me@winterminute.com.NOSPAM> wrote in message
news:u1**************@TK2MSFTNGP11.phx.gbl...
Yes, this helps very much. What if I didn't want that column to be a
calculation based on other columns (as your example shows), but rather
additional information for that record.

Is there something other than "myDataColumn.expression=" that I could set
the value of the new column to a string value?
"vMike" <Mi****************@noYandZ.geZwaYrrenZ.com> wrote in message
news:bu**********@ngspool-d02.news.aol.com...
Here is an example of adding a column to a datatable this is based on two
other columns

Dim myDataColumn As DataColumn= new datacolumn("ShipDisplay")
myDataColumn.datatype=System.Type.GetType("System. String")
myDataColumn.expression = ("Description + ' cost $' + Charge")
dt.columns.add(myDatacolumn)

Please note that the expression not an asp.net statement, it is like a
database statement. In the above example the expression is the data in the Description column concatenated with the word 'cost $' and data in the
Charge column. So if I had and row item in my table with the Description
'Express' and the Charge column was "20" then the new column would display 'Express cost $20' for that data row. I don't think you can use variables (except for constant strings) in the expression statement. You can use

math
and other functions like you would in a database statement but it has to

be
based on the columns of the data base.

Hope this helps.

"Robin Thomas" <me@winterminute.com.NOSPAM> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple example would be: Let's say Column1=StartDate and Column2=EndDate. In addition to displaying Column1 and
Column2, I need to do some calculations and display in as Column3.

The calculations are easy and can be done in the code-behind. How to

display
it is another question.
However, they might not be math calculations. Take for example, a SQL

query
that returns a list of servers and then for each server I use WMI to
get some additional information and then I need to display the servername

(from
SQL) and the OS and Service Pack Version (from WMI) on a web page.

The way I have been handling it is creating a new table in memory and

adding
the columns from the original SQL query and then doing the
calculations and
adding that as a column. Then I'll use this new table to DataBind my
DataGrid or DataList.

Is this the best way to do it? I feel like this isn't the smartest way

to acomplish this.



Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by 6thirty | last post: by
13 posts views Thread by Shannan Casteel via AccessMonster.com | last post: by
10 posts views Thread by Trevor | last post: by
8 posts views Thread by rriness | last post: by
16 posts views Thread by Geoff Jones | last post: by
14 posts views Thread by Karl Irvin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.