Solved

I appreciate any help to extract data from a datatable's first row and last row.

Posted on 2007-04-04
6
221 Views
Last Modified: 2010-04-15
I need help to get started on the method I created. I appreciate any help.
        Public Function GetCustomerListForDatePeriods(ByVal dbName As String, ByVal dt As DataTable) As DataSet

            ' Read First entry from DataTable   2004 01   2004 01 29  2004 02 25  three entrees
            ' Get the second and third entry of the first row of the datatable
            ' Second Entry will be DT_FROM and third entry will be DT_TO_MONTH
            ' Read Last entry from  DataTable   2005 03   2005 0X 0X  2003 03 03
            ' So we need the first row and last row of the datatable
            ' Third entry of the last row

            DataRow row;

             // Get last row
             row = (DataRow)table.Rows[0];
             we need to put the second column and third in variables.
             row = (DataRow)table.Rows[table.Rows.Count-1];
             we need to get third column of the last row.
             I need help to setup this function.

            Dim retVal As New DataTable("Customers")
            Dim cmd As DbCommand
            Dim tmpds As DataSet
            Dim StoredProcName As String = String.Empty

            Try
                StoredProcName = dbName & ".dbo.usp_GetPrestigeCustomers"

                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                cmd.CommandTimeout = 0


                dbManager.AddInParameter(cmd, "@DT_FROM", DbType.DateTime, )        ' Second Entry will be of first row
                dbManager.AddInParameter(cmd, "@DT_TO", DbType.DateTime, )          ' Third entry of the last row
                dbManager.AddInParameter(cmd, "@DT_TO_MONTH", DbType.DateTime, )    ' Third entry will be of first row


                'Temporarilly put the results in a dataset so we can check for valid data
                tmpds = dbManager.ExecuteDataSet(cmd)

                'We must ensure that only one table was returned in the execution of the query
                If tmpds.Tables.Count.Equals(1) Then

                    'We also need to ensure that there is data
                    If tmpds.Tables(0).Rows.Count > 0 Then
                        retVal = tmpds.Tables(0)
                    Else
                        '  Return retVal
                    End If
                Else
                    ' Return retVal
                End If

            Catch ex As Exception
                Throw ex
            Finally
                'Cleaning up
                If Not tmpds Is Nothing Then
                    tmpds.Dispose()
                End If

                If Not cmd Is Nothing Then
                    cmd.Dispose()
                End If

            End Try

            '      Return retVal

        End Function
0
Comment
Question by:mathieu_cupryk
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18852182
I'm sorry but I think it is not enough inforamtion in your question and code.
In addition, it look like VB code, not C# code.
0
 

Author Comment

by:mathieu_cupryk
ID: 18852246
Public Function GetCustomerListForDatePeriods(ByVal dbName As String, ByVal dt As DataTable) As DataSet

            ' Read First entry from DataTable   2004 01   2004 01 29  2004 02 25  three entrees
            ' Get the second and third entry of the first row of the datatable
            ' Second Entry will be DT_FROM and third entry will be DT_TO_MONTH
            ' Read Last entry from  DataTable   2005 03   2005 0X 0X  2003 03 03
            ' So we need the first row and last row of the datatable
            ' Third entry of the last row
            Dim dr As DataRow

            If (dt.Rows.Count > 0) Then

                dr = dt.Rows(dt.Rows.Count - 1)

            End If


            If (dt.Rows.Count > 0) Then

                dr = dt.Rows(0)  ' Brings back first row

            End If
Now I just need to put the above in three variables.

and pass it in

  '   dbManager.AddInParameter(cmd, "@DT_FROM", DbType.DateTime, )        ' Second Entry will be of first row
                '  dbManager.AddInParameter(cmd, "@DT_TO", DbType.DateTime, )          ' Third entry of the last row
                '  dbManager.AddInParameter(cmd, "@DT_TO_MONTH", DbType.DateTime, )    ' Third entry will be of first row
Does not matter c# or vb.net
0
 

Author Comment

by:mathieu_cupryk
ID: 18852505
Get the values for the paramaters.

       Public Function GetCustomerListForDatePeriods(ByVal dbName As String, ByVal dt As DataTable) As DataSet

            ' Read First entry from DataTable   2004 01   2004 01 29  2004 02 25  three entrees
            ' Get the second and third entry of the first row of the datatable
            ' Second Entry will be DT_FROM and third entry will be DT_TO_MONTH
            ' Read Last entry from  DataTable   2005 03   2005 0X 0X  2003 03 03
            ' So we need the first row and last row of the datatable
            ' Third entry of the last row
            Dim dr1 As DataRow
            Dim dr2 As DataRow


            If (dt.Rows.Count > 0) Then

                dr1 = dt.Rows(dt.Rows.Count - 1)

            End If


            If (dt.Rows.Count > 0) Then

                dr2 = dt.Rows(0)  ' Brings back first row

            End If



            Dim retVal As New DataTable("Customers")
            Dim cmd As DbCommand
            Dim tmpds As DataSet
            Dim StoredProcName As String = String.Empty

            Try
                StoredProcName = dbName & ".dbo.usp_GetPrestigeCustomers"

                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                cmd.CommandTimeout = 0


                dbManager.AddInParameter(cmd, "@DT_FROM", DbType.DateTime, dr1(2))           ' Second Entry will be of first row
                dbManager.AddInParameter(cmd, "@DT_TO", DbType.DateTime, dr2(3))             ' Third entry of the last row
                dbManager.AddInParameter(cmd, "@DT_TO_MONTH", DbType.DateTime, dr1(3))       ' Third entry will be of first row


                'Temporarilly put the results in a dataset so we can check for valid data
                tmpds = dbManager.ExecuteDataSet(cmd)

                'We must ensure that only one table was returned in the execution of the query
                If tmpds.Tables.Count.Equals(1) Then

                    'We also need to ensure that there is data
                    If tmpds.Tables(0).Rows.Count > 0 Then
                        retVal = tmpds.Tables(0)
                    Else
                        '  Return retVal
                    End If
                Else
                    ' Return retVal
                End If

            Catch ex As Exception
                Throw ex
            Finally
                'Cleaning up
                If Not tmpds Is Nothing Then
                    tmpds.Dispose()
                End If

                If Not cmd Is Nothing Then
                    cmd.Dispose()
                End If

            End Try

            '      Return retVal

        End Function
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 16

Accepted Solution

by:
AlexNek earned 500 total points
ID: 18852551
I only want to hint you that in VB zone you can possible get better answer.
What was wrong for me that in your text column called as entry. In addition, only in stuped homework we need to know 1-st and last DB record.

As you get the row you need to get the right column
http://msdn2.microsoft.com/en-us/library/system.data.datarow.item.aspx
dt[0] ot dt.Item(0) I think gor the frist one.
0
 

Author Comment

by:mathieu_cupryk
ID: 18852663
Where is the VB zone I the link ?
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18853233
I don't know zones for asker but for me it is here
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/#browseZones
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now