Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private Sub btnShowCash_Click(sender As Object, e As EventArgs) Handles btnShowCash.Click
Try

con = New OleDbConnection(cs)

con.Open()
cmd = New OleDbCommand("SELECT (BillNo) as [Bill No],(BillDate) as [Bill Date],(PatientID) as [Patient ID],(Total) as [Total],(TotalPayment) as [Total Payment],(PaymentDue) as [Payment Due] from BillingInfo where BillDate between #" & GetFromDate.Text & "# And #" & GetToDate.Text & "#", con)
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

Dim myDataSet As DataSet = New DataSet()

myDA.Fill(myDataSet, "BillingInfo")

DataGridView1.DataSource = myDataSet.Tables("BillingInfo").DefaultView
Dim sum As Int64 = 0
Dim sum1 As Int64 = 0
Dim sum2 As Int64 = 0


For Each r As DataGridViewRow In Me.DataGridView1.Rows
sum = sum + r.Cells(4).Value
sum1 = sum1 + r.Cells(5).Value
sum2 = sum2 + r.Cells(6).Value
Next
txtTotal.Text = sum
txtTotalPayment.Text = sum1
txtPaymentDue.Text = sum2

con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Posted

1 solution

Cell indexes start from 0 (zero) so from your query (6 columns) you can have maximum index of 5. It would be better if you access the cells via column names.

If this helps, please take time to accept the solution. Thank you.


Also, next time be so kind and a) format the code and b) indicate exact line in which you get the error (I'd say Cells(6) since that is impossible one :) but still...

Better solution:
VB
sum = myDataSet.Tables("BillingInfo").Compute("SUM(Total)"), "");
sum1 = myDataSet.Tables("BillingInfo").Compute("SUM(TotalPayment)"), "");
sum2 = myDataSet.Tables("BillingInfo").Compute("SUM(PaymentDue)"), ""); // why this?


Also, consider explicitely casting to Int64 via TryCast and renaming sum, sum1 and sum2 to something more descriptive. You'll be thankful to yourself in 6 months when you check the code and wonder that sum2 is even if it is clear now :)
 
Share this answer
 
v2

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