Min and Max ADO.NET

  • Thread starter Thread starter 11Oppidan
  • Start date Start date
1

11Oppidan

Hello,

What is the simplest way to get the minimum and maximum values from a
specific column of data in a populated datatable in a dataset, using ADO.
NET and VB.NET.

Many thanks for your help
 
Thanks that works but when I try as per the example on microsoft.com

dim objSum as object
objSum = DataTable.Compute("Sum([Column Name1])","[Column Name2] = 2")

I get an error: "Cannot perfomr '=' operation on System.String and
System.Int32"
I don't understand what I am doing wrong. Any help much appreciated.



Miha Markic said:
Hi,

Use DataTable.Compute("Min(ColumnName)", string.Empty); or Max

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

11Oppidan said:
Hello,

What is the simplest way to get the minimum and maximum values from a
specific column of data in a populated datatable in a dataset, using ADO.
NET and VB.NET.

Many thanks for your help
 
Hi 11Oppidan,

You're getting this error because the Column Name2 column is a string type
column. So we cannot use = to 2. We have to add single quotes between 2.
Please try the following instead.

objSum = DataTable.Compute("Sum([Column Name1])","[Column Name2] = '2'")

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks for your assitance Kevin - that works great.

The synatax on
http://msdn.microsoft.com/library/d...frlrfSystemDataDataTableClassComputeTopic.asp
is misleading - see below for the example it provides...

[Visual Basic]
Private Sub ComputeBySalesSalesID(ByVal myDataSet As DataSet)
' Presumes a DataTable named "Orders" that has a column named "Total."
Dim myTable As DataTable
myTable = myDataSet.Tables("Orders")
' Declare an object variable.
Dim objSum As Object
objSum = myTable.Compute("Sum(Total)", "EmpID = 5")
End Sub
 
You're welcome.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top