Solved

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

Posted on 2007-04-04
6
238 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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