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

  • Thread starter Thread starter Robin Thomas
  • Start date Start date
R

Robin Thomas

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.
 
An expression column is generally the best way to do this
http://www.ondotnet.com/pub/a/dotnet/2003/05/26/datacolumn_expressions.html
but
that may not be appropriate here. Expression columns will automatically
show updates when values changes...

Anyway, you could add a DataColumn (just like you would a button column,
hyperlink column or checkbox column) and just add the data to each cell in
the column

foreach(DataRow dr in myTable.Rows){
dr["ColumnYouJustAdded"] = WMICall;

}

The solution really depends on what the calculation is going to be based on,
but if you can't use an expression column (and it looks like in this
instance you probably can't) you can add a DataColumn and programatically
set each row value in that column.

HTH,

Bill
 
Back
Top