Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2007-04-04
6
245 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Service Controller 1 27
Add different cell to otherwise similiar row 4 45
Expression Evaluater 3 37
Unlocking a column in excel using C# 17 18
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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