5
Answers

cannot get the sum total of amount paid in school billing system

Photo of Ahmet Taha

Ahmet Taha

4y
523
1
hello guyz, i need your help how do i get the sum total of amount paid (cashreceived) of student's payment, thanks a lot.
 
 

Answers (5)

4
Photo of Rijwan Ansari
5 66.3k 3m 4y
Hi,
 
You are selecting only one record as sum so use dr.GetDecimal(0) as shown
 
  1. txttotalamountpaid.Text = Convert.ToString(dr.GetDecimal(0));  
 
3
Photo of Nitin Sontakke
138 13.6k 15.4k 4y
Hello,
 
Few things to note here:
 
1/ It should be dr.GetInt32(0).ToString() as it is the first column in the resultset.
 
2/ Make sure the returning value is of data type Int. Your SQL Server column is decimal, hence you should be requesting a Decimal column.
 
3/ Because you are getting just one value, you are better of using ExecuteScalar instead of ExecuteReader.
 
4/ Try to avoid directly accessing database in your application. Use layered approach. Abstract functionality and use objects to fetch data.
 
5/ Pretty much all your columns are nvarchar(50). This does not look good. Use correct data types. 
 
 
 
2
Photo of Jignesh Kumar
30 39.6k 2.9m 4y
Hi,
 
Please refer this thread,
https://stackoverflow.com/questions/53065091/how-to-get-number-value-with-decimal-from-datareader 
2
Photo of Salman Beg
158 12.1k 624.9k 4y
The reason why it is showing indexoutofrange exception because in dr.GetInt32(6) the index 6 is not present. So you can use the index which is present. Thanks
2
Photo of Hiesh Kidecha
NA 241 3.7k 4y
Because datatype of CashReceived column is Decimal(18,2), you should use the "GetDecimal" method of SqlDataReader. A SqlDataReader object implmements the IDataRecord interface. Hence, it implements many methods. Among these methods, there is the GetDecimal.
 
Try this:
txttotalamountpaid.Text = Convert.ToString(dr.GetDecimal(0));