Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading with an sqlreader in vb.net.  I want to sum for multiple records, one member number.  How do I keep a member number while continuing to read the file?

Posted on 2009-05-13
4
Medium Priority
?
223 Views
Last Modified: 2012-05-06
I want to sum for multiple records, one member number.  How do I keep a member number while continuing to read the file?  
read in a record, assign member_nbr to HoldMember_Nbr
process that record
read in a new record, check if Member_Nbr = HoldMember_Nbr
If it does not process that record
If it does write record to a new file.

Dim Member_Nbr, CurrentMbrNbr, Share_Nbr, TransType, DAATMWCNT, DACUATMWCT, DAFCCNT, DAATMWCNT37, DACUATMWCT37, DAFCCNT27, DAFCCNT37 As Integer
        Dim TransDesc, NetOrg, TermID As String
        Member_Nbr = 0
        CurrentMbrNbr = 0
        Share_Nbr = 0
        TransType = 0
        TermID = ""
        TransDesc = ""
        NetOrg = ""
        DAATMWCNT = 0
        DACUATMWCT = 0
        DAFCCNT = 0
        DAATMWCNT37 = 0
        DACUATMWCT37 = 0
        DAFCCNT27 = 0
        DAFCCNT37 = 0
 
        Dim TogetherFinalSortedquery As String
 
        TogetherFinalSortedquery = "SELECT dbo.TogetherFinalSorted.MemberNumber,dbo.TogetherFinalSorted.TransDesc,dbo.TogetherFinalSorted.TransType, " & _
        "dbo.TogetherFinalSorted.TermID,dbo.TogetherFinalSorted.NetOrg " & _
        "FROM TogetherFinalSorted " & _
        "ORDER BY dbo.TogetherFinalSorted.MemberNumber asc"
 
        Using TogetherFinalSortedconnstring As New SqlConnection(dxp2connstring)
            Dim TogetherFinalSortedreadercommand As New SqlCommand(TogetherFinalSortedquery, TogetherFinalSortedconnstring)
            Try
                TogetherFinalSortedconnstring.Open()
                Dim TogetherFinalSortedreader1 As SqlDataReader = TogetherFinalSortedreadercommand.ExecuteReader()
 
                While TogetherFinalSortedreader1.Read()
                    Dim TogetherFinalSortedAppend As New SqlConnection(dxp2connstring)
 
                    Try
                        TogetherFinalSortedAppend.Open()
                    Catch E15 As Exception
                        MsgBox(E15.Message)
                    End Try
 
                    If CurrentMbrNbr <> TogetherFinalSortedreader1(0) Then
                        GoTo AddRecord
                    Else
                        CurrentMbrNbr = TogetherFinalSortedreader1(0)
                        TransDesc = TogetherFinalSortedreader1(1)
                        TransType = TogetherFinalSortedreader1(2)
                        TermID = TogetherFinalSortedreader1(3)
                        NetOrg = TogetherFinalSortedreader1(4)
                    End If
 
                    If (Not (Trim(TransDesc) Like "CR ADJ") And Not (Trim(TransDesc) Like "DEPOSIT") And Not (Trim(TransDesc) Like "TFR DEP")) Then
 
                        If FindTermID(TermID) Then
                            If TransType = 27 Then
                                DACUATMWCT = DACUATMWCT + 1
                            Else
                                DACUATMWCT37 = DACUATMWCT37 + 1
                            End If
                        Else
                            If ((Not Trim(TransDesc) Like "RECUR" And Not (TransDesc) Like "PURCHSE" And Not (TransDesc) Like "WD REV ") And TransType Like "27") Then
                                DAATMWCNT = DAATMWCNT + 1
                            Else
                                If ((Not (TransDesc) Like "RECUR" And Not (TransDesc) Like "PURCHSE" And Not (TransDesc) Like "WD REV ") And TransType <> "27") Then
                                    DAATMWCNT37 = DAATMWCNT37 + 1
                                Else
                                    If TransDesc Like "PURCHSE" And (Not (NetOrg) Like "VSA" And Not (NetOrg) Like "VSI" And Not (NetOrg) Like "VSB") And TransType Like "27" Then
                                        DAATMWCNT = DAATMWCNT + 1
                                    Else
                                        If TransDesc Like "PURCHSE" And (Not (NetOrg) Like "VSA" And Not (NetOrg) Like "VSI") And Not (NetOrg) Like "VSB" And TransType <> "27" Then
                                            DAATMWCNT37 = DAATMWCNT37 + 1
                                        Else
                                            If (Trim(TransDesc) Like "PURCHSE" Or Trim(TransDesc) Like "RECUR") Or (NetOrg Like "VSA" Or (NetOrg) Like "VSI" Or (NetOrg) Like "VSB") Then
                                                If TransType = 27 Then
                                                    DAFCCNT27 = DAFCCNT27 + 1
                                                Else
                                                    DAFCCNT37 = DAFCCNT37 + 1
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
AddRecord:
                    If Member_Nbr = TogetherFinalSortedreader1(0) Then
                        GoTo SameMember
                    Else
                        If DAATMWCNT > 0 Or DACUATMWCT > 0 Or DAFCCNT27 > 0 Then
 
                            Member_Nbr = CurrentMbrNbr
                            Share_Nbr = 2
                            DAATMWCNT = DAATMWCNT
                            DACUATMWCT = DACUATMWCT
                            DAFCCNT = DAFCCNT27
 
                        End If
                        If (DAATMWCNT37 + DACUATMWCT37) > 0 Or Not (DAATMWCNT37 + DACUATMWCT37) = 0 Then
                            Member_Nbr = CurrentMbrNbr
                            Share_Nbr = 1
                            DAATMWCNT = DAATMWCNT37
                            DACUATMWCT = DACUATMWCT37
                            DAFCCNT = DAFCCNT37
 
                        End If
 
                        Dim FeeJobCountTableAppendCommand2 As String = "SELECT * FROM FeeJobCountTable"
                        Dim FeeJobCountTableAppendadapter2 As SqlDataAdapter = New SqlDataAdapter(FeeJobCountTableAppendCommand2, TogetherFinalSortedconnstring)
                        FeeJobCountTableAppendadapter2.SelectCommand = New SqlCommand(FeeJobCountTableAppendCommand2, TogetherFinalSortedconnstring)
 
                        Dim FeeJobCountTableAppendQuery2 As String = "insert into FeeJobCountTable " & _
                                                "(Member_NBR,Share_Nbr,DAATMWCNT,DACUATMWCT,DAFCCNT)" & _
                                                "VALUES (@Member_NBR,@Share_Nbr,@DAATMWCNT,@DACUATMWCT, @DAFCCNT)"
 
                        FeeJobCountTableAppendadapter2.InsertCommand = New SqlCommand(FeeJobCountTableAppendQuery2, TogetherFinalSortedconnstring)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@MemberNumber", SqlDbType.BigInt, 8).Value = TogetherFinalSortedreader1(0)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@Share_Nbr", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(1)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@DAATMWCNT", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(2)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@DACUATMWCT", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(3)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@DAFCCNT", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(4)
                        Using TogetherFinalSortedAppend
                            Try
                                Dim EntryDateAndTranDatewriterecordsAffected As Int32 = FeeJobCountTableAppendadapter2.InsertCommand.ExecuteNonQuery()
                            Catch ex As Exception
                                MsgBox(ex.Message)
                            End Try
                        End Using
                        TogetherFinalSortedAppend.Close()
 
                        Member_Nbr = 0
                        CurrentMbrNbr = 0
                        Share_Nbr = 0
                        TransType = 0
                        TermID = ""
                        TransDesc = ""
                        NetOrg = ""
                        DAATMWCNT = 0
                        DACUATMWCT = 0
                        DAFCCNT = 0
                        DAATMWCNT37 = 0
                        DACUATMWCT37 = 0
                        DAFCCNT27 = 0
                        DAFCCNT37 = 0
 
                    End If
SameMember:
                End While
 
                '***************************************************************************************************************************
            Catch ex1 As Exception
                MsgBox(ex1.Message)
            End Try
            TogetherFinalSortedconnstring.Close()
 
        End Using
 
        MsgBox("Shazam Process Files Created.")

Open in new window

0
Comment
Question by:ConnexusDave
[X]
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
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 24376263
You store in a variable that will only change when you detect that the value on the variable is different than the value on the current record
0
 

Author Comment

by:ConnexusDave
ID: 24376697
Where in the code do you store the variable, the way I have it now it changes everytime you read a new record?  I am used to Access code and does this routine different than vb.net.  I have always used next record, but vb.net does not have (as far as I know) this function.
0
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 2000 total points
ID: 24381193
Try this:

Dim Member_Nbr, CurrentMbrNbr, Share_Nbr, TransType, DAATMWCNT, DACUATMWCT, DAFCCNT, DAATMWCNT37, DACUATMWCT37, DAFCCNT27, DAFCCNT37 As Integer
        Dim TransDesc, NetOrg, TermID As String
        Member_Nbr = 0
        CurrentMbrNbr = 0
        Share_Nbr = 0
        TransType = 0
        TermID = ""
        TransDesc = ""
        NetOrg = ""
        DAATMWCNT = 0
        DACUATMWCT = 0
        DAFCCNT = 0
        DAATMWCNT37 = 0
        DACUATMWCT37 = 0
        DAFCCNT27 = 0
        DAFCCNT37 = 0
 
        Dim TogetherFinalSortedquery As String
 
        TogetherFinalSortedquery = "SELECT dbo.TogetherFinalSorted.MemberNumber,dbo.TogetherFinalSorted.TransDesc,dbo.TogetherFinalSorted.TransType, " & _
        "dbo.TogetherFinalSorted.TermID,dbo.TogetherFinalSorted.NetOrg " & _
        "FROM TogetherFinalSorted " & _
        "ORDER BY dbo.TogetherFinalSorted.MemberNumber asc"
 
        Using TogetherFinalSortedconnstring As New SqlConnection(dxp2connstring)
            Dim TogetherFinalSortedreadercommand As New SqlCommand(TogetherFinalSortedquery, TogetherFinalSortedconnstring)
            Try
                TogetherFinalSortedconnstring.Open()
                Dim TogetherFinalSortedreader1 As SqlDataReader = TogetherFinalSortedreadercommand.ExecuteReader()
 
                While TogetherFinalSortedreader1.Read()
                    Dim TogetherFinalSortedAppend As New SqlConnection(dxp2connstring)
 
                    Try
                        TogetherFinalSortedAppend.Open()
                    Catch E15 As Exception
                        MsgBox(E15.Message)
                    End Try
 
                        CurrentMbrNbr = TogetherFinalSortedreader1(0)
                        TransDesc = TogetherFinalSortedreader1(1)
                        TransType = TogetherFinalSortedreader1(2)
                        TermID = TogetherFinalSortedreader1(3)
                        NetOrg = TogetherFinalSortedreader1(4)
 
                    If CurrentMbrNbr <> Member_Nbr Then
                        Member_Nbr = CurrentMbrNbr
                        GoTo AddRecord
                    End If
 
                    If (Not (Trim(TransDesc) Like "CR ADJ") And Not (Trim(TransDesc) Like "DEPOSIT") And Not (Trim(TransDesc) Like "TFR DEP")) Then
 
                        If FindTermID(TermID) Then
                            If TransType = 27 Then
                                DACUATMWCT = DACUATMWCT + 1
                            Else
                                DACUATMWCT37 = DACUATMWCT37 + 1
                            End If
                        Else
                            If ((Not Trim(TransDesc) Like "RECUR" And Not (TransDesc) Like "PURCHSE" And Not (TransDesc) Like "WD REV ") And TransType Like "27") Then
                                DAATMWCNT = DAATMWCNT + 1
                            Else
                                If ((Not (TransDesc) Like "RECUR" And Not (TransDesc) Like "PURCHSE" And Not (TransDesc) Like "WD REV ") And TransType <> "27") Then
                                    DAATMWCNT37 = DAATMWCNT37 + 1
                                Else
                                    If TransDesc Like "PURCHSE" And (Not (NetOrg) Like "VSA" And Not (NetOrg) Like "VSI" And Not (NetOrg) Like "VSB") And TransType Like "27" Then
                                        DAATMWCNT = DAATMWCNT + 1
                                    Else
                                        If TransDesc Like "PURCHSE" And (Not (NetOrg) Like "VSA" And Not (NetOrg) Like "VSI") And Not (NetOrg) Like "VSB" And TransType <> "27" Then
                                            DAATMWCNT37 = DAATMWCNT37 + 1
                                        Else
                                            If (Trim(TransDesc) Like "PURCHSE" Or Trim(TransDesc) Like "RECUR") Or (NetOrg Like "VSA" Or (NetOrg) Like "VSI" Or (NetOrg) Like "VSB") Then
                                                If TransType = 27 Then
                                                    DAFCCNT27 = DAFCCNT27 + 1
                                                Else
                                                    DAFCCNT37 = DAFCCNT37 + 1
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
GoTo SameMember
AddRecord:
                        If DAATMWCNT > 0 Or DACUATMWCT > 0 Or DAFCCNT27 > 0 Then
 
                            Member_Nbr = CurrentMbrNbr
                            Share_Nbr = 2
                            DAATMWCNT = DAATMWCNT
                            DACUATMWCT = DACUATMWCT
                            DAFCCNT = DAFCCNT27
 
                        End If
                        If (DAATMWCNT37 + DACUATMWCT37) > 0 Or Not (DAATMWCNT37 + DACUATMWCT37) = 0 Then
                            Member_Nbr = CurrentMbrNbr
                            Share_Nbr = 1
                            DAATMWCNT = DAATMWCNT37
                            DACUATMWCT = DACUATMWCT37
                            DAFCCNT = DAFCCNT37
 
                        End If
 
                        Dim FeeJobCountTableAppendCommand2 As String = "SELECT * FROM FeeJobCountTable"
                        Dim FeeJobCountTableAppendadapter2 As SqlDataAdapter = New SqlDataAdapter(FeeJobCountTableAppendCommand2, TogetherFinalSortedconnstring)
                        FeeJobCountTableAppendadapter2.SelectCommand = New SqlCommand(FeeJobCountTableAppendCommand2, TogetherFinalSortedconnstring)
 
                        Dim FeeJobCountTableAppendQuery2 As String = "insert into FeeJobCountTable " & _
                                                "(Member_NBR,Share_Nbr,DAATMWCNT,DACUATMWCT,DAFCCNT)" & _
                                                "VALUES (@Member_NBR,@Share_Nbr,@DAATMWCNT,@DACUATMWCT, @DAFCCNT)"
 
                        FeeJobCountTableAppendadapter2.InsertCommand = New SqlCommand(FeeJobCountTableAppendQuery2, TogetherFinalSortedconnstring)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@MemberNumber", SqlDbType.BigInt, 8).Value = TogetherFinalSortedreader1(0)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@Share_Nbr", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(1)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@DAATMWCNT", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(2)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@DACUATMWCT", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(3)
                        FeeJobCountTableAppendadapter2.InsertCommand.Parameters.Add("@DAFCCNT", SqlDbType.Int, 4).Value = TogetherFinalSortedreader1(4)
                        Using TogetherFinalSortedAppend
                            Try
                                Dim EntryDateAndTranDatewriterecordsAffected As Int32 = FeeJobCountTableAppendadapter2.InsertCommand.ExecuteNonQuery()
                            Catch ex As Exception
                                MsgBox(ex.Message)
                            End Try
                        End Using
                        TogetherFinalSortedAppend.Close()
 
SameMember:
                End While
 
                '***************************************************************************************************************************
            Catch ex1 As Exception
                MsgBox(ex1.Message)
            End Try
            TogetherFinalSortedconnstring.Close()
 
        End Using
 
        MsgBox("Shazam Process Files Created.")

Open in new window

0
 

Author Closing Comment

by:ConnexusDave
ID: 31581028
Thank you
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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