Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi can any one help me to give me a suggestion how to get alias column value from i'm trying to use a for each loop in vb.net using a datatable.

My code is actually shown below
VB
If CInt(RowItem.Item("TOTALLINENO"))  500 Then
       For c As Integer = 0 To tblRawTesterShutdown.Columns.Count - 1
If CInt(tblRawTesterShutdown.Rows(c)(10)("Percentage")); Max Then
       Max = CInt(tblRawTesterShutdown.Rows(c)(10)("Percentage"))
              End If
        Next
  Max = Max - Convert.ToInt32(tblRawTesterShutdown.Rows(tblRawTesterShutdown.Rows.Count - 1))
                  Else
                   Return False
   End If 'End here

I have do it in vs 2005. The percentage is a sql server query. the query is shown below
VB
SELECT  t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.FAMILY, t1.HSA_STATUS,t1.CACHE_FAMILY, 
    SUM(t1.TOTAL) AS CountOfLineNo, t2.TotalOfLineNo, 
    convert(decimal(18,3),(convert(decimal(8,1),SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo )/100)AS Percentage
    FROM(
          SELECT LINE_NO, E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY, Count(LINE_NO) as Total
          FROM TX_HSA_SUMM WITH(NOLOCK)
          WHERE MT_TIMESTAMP2 BETWEEN ('2013-04-03 14:28:00.000') AND ('2013-04-03 14:28:00.000')
          AND  E_FIELD!='00000' AND F_FIELD!='00000' AND G_FIELD!='00000' -- added new line
          GROUP BY LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY
        ) AS t1 
    LEFT JOIN (
          SELECT LINE_NO, COUNT(LINE_NO) AS TotalOfLineNo
          FROM TX_HSA_SUMM WITH(NOLOCK)
          WHERE MT_TIMESTAMP2 BETWEEN ('2013-04-03 14:28:00.000') AND ('2013-04-03 14:28:00.000')
          GROUP BY LINE_NO 
          ) AS t2 
    ON t1.LINE_NO = t2.LINE_NO    
	GROUP BY t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, t1.Total,t2.TotalOfLineNo
	HAVING t2.TotalOfLineNo > '500' and t1.line_no='64072m' and hsa_status='r'
	ORDER BY   t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t2.TotalOfLineNo,t1.Total,t1.HSA_STATUS, t1.LINE_NO, t1.FAMILY, t1.CACHE_FAMILY

The bold Percentage value from sql server should be used in the vb.net for each loop actually. I'm using vs 2005 ans sql server 2000. Thanks in advance
Posted
Updated 2-Apr-13 22:07pm
v3

1 solution

If i good remember, you want to read values stored in a DataTable object to sort records and do some other operations, because MS SQL server functionality is less than MS SQL Server 2005 and does not contains functions like ROW_NUMBER...

You want to go through the collection of records (stored in single column) to get a maximum, then to do some operations.
You need to "convert" datatable object into List(of T)[^], which contains functionality you're looking for.

VB
Module Module1

    Sub Main()
        Dim dt As DataTable = Nothing, dr As DataRow = Nothing, dc As DataColumn = Nothing
        Dim dl As List(Of Integer) = Nothing, i As Integer = 0

        Try
            dt = CreateDataTable()
            dc = dt.Columns("MyInt")
            dl = New List(Of Integer)
            For Each dr In dt.Rows
                dl.Add(dr.Item(dc))
            Next

            Console.WriteLine("=== Unsorted list ===")
            For i = 0 To dl.Count - 1
                Console.WriteLine("Item: {0}, value: {1}", i, dl.Item(i))
            Next

            Console.WriteLine("")
            Console.WriteLine("Sorting...")
            dl.Sort()
            Console.WriteLine("")
            Console.WriteLine("=== Sorted list ===")
            For i = 0 To dl.Count - 1
                Console.WriteLine("Item: {0}, value: {1}", i, dl.Item(i))
            Next

            Console.ReadKey()

        Catch ex As Exception

        End Try


    End Sub

    Function CreateDataTable() As Data.DataTable
        Dim dt As Data.DataTable = Nothing
        Dim dr As Data.DataRow = Nothing
        Dim dc As Data.DataColumn = Nothing
        Dim i As Integer = 0, rnd As Random = Nothing
        Try
            rnd = New Random
            dt = New Data.DataTable("Test")
            dc = New DataColumn("MyInt", GetType(Integer))
            dt.Columns.Add(dc)
            For i = 1 To 20
                dr = dt.NewRow
                dr.Item("MyInt") = rnd.Next(1, 1000)
                dt.Rows.Add(dr)
            Next

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            dc = Nothing
            dr = Nothing
            rnd = Nothing
        End Try

        Return dt
    End Function

End Module
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900