I want to sum up my Amount column, but I receive this error message
Additional information: Index was outside the bounds of the array.
Here is the definition of the Balance table from SQL database:
CREATE TABLE [dbo].[Balance]
(
[BalanceID] [int] IDENTITY(1,1) NOT NULL,
[Sn] [nvarchar](50) NULL,
[Description] [nvarchar](1000) NULL,
[Date] [date] NULL,
[Amount] [decimal](8, 0) NULL
)
Here is the code C# that accesses the table via SQL inline
cn.Open();
SqlCommand cmd = cn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select sum(Amount) from Balance where Sn=@sn and Date Between @SD and @ED";
cmd.Parameters.Add(new SqlParameter("@sn", txtSn.Text));
cmd.Parameters.Add(new SqlParameter("@SD", DTPStart.Text));
cmd.Parameters.Add(new SqlParameter("@ED", DTPEnd.Text));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Debt = reader.GetDecimal(4);
}
reader.Close();
cn.Close();
Any help will be much appreciated!