Hi,
I have been trying to calculate the sum of a particular column in database and display the result in a label control. The column is set as Nvarchar(max). I also need to search for and display the sum based on two dates. I only have one date column in the database. When a user adds dates which he or she wants to search between, the user will input the "FROM" AND "TO" dates in the provided boxes and clicks on search button and the total sum of the figures according to the dates will show in a label.
The issue I have is that the total calculated sum is not correct because I added all the the figures manually and it does not rhyme with the calculated one:
Please what should I do here? Could it be that the date in the input control type "date" is different from the dababase?
Table Example
Id(int) Uid(NVARCHAR(MAX) due(NVARCHAR(MAX) DateCreated (NVARCHAR(MAX)
1 1 59000.00 Oct 31 2024 1:39AM
2 2 280000.00 Nov 17 2024 1:39AM
3 2 350000.00 Nov 20 2024 1:39AM
4 2 3000.00 Nov 21 2024 1:39AM
5 3 4300 Nov 21 2024 1:39AM
6 2 580000 Nov 23 2024 1:39AM
HTML
<div class="row col-sm-11" style="margin-right: auto; margin-left: auto;">
<asp:Label ID="createdby" runat="server" Visible="false" Text="2"></asp:Label>
<div class="col-sm-1">
<label style="font-weight: 500; color: #5f5f5f;">FROM</label>
</div>
<div class="col-sm-4">
<input type="datetime-local" id="datepicker" name="dateclosed" runat="server" class="form-control" style="font-size: 9pt" placeholder="Select date" />
</div>
<div class="col-sm-1">
<label style="font-weight: 500; color: #5f5f5f;">TO</label>
</div>
<div class="col-sm-4">
<input type="datetime-local" id="datepicker1" name="dateclosed" runat="server" class="form-control" style="font-size: 9pt" placeholder="Select date" />
</div>
<div class="col-sm-2">
<asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Text="Search" OnClick="Button2_Click" />
</div>
<br /><br />
<div style="text-align: center;">
<asp:Label ID="Label1" runat="server" Text="500"></asp:Label>
</div>
</div>
C#
protected void Button2_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection())
{
int count = 0;
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT ISNULL(SUM(CAST(due as money)),0) FROM TableRecords WHERE Uid = @Uid AND CAST(DateCreated as DATE) BETWEEN @StartDate AND @EndDate";
cmd.Parameters.AddWithValue("@Uid", createdby.Text);
cmd.Parameters.AddWithValue("@StartDate", DateTime.Parse(datepicker.Value));
cmd.Parameters.AddWithValue("@EndDate", DateTime.Parse(datepicker1.Value));
cmd.Connection = con;
con.Open();
count = Convert.ToInt32(cmd.ExecuteScalar());
Label1.Text = count.ToString("###,##0");
con.Close();
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
Could it be that the line of code in below is not done right, that's why I am not getting the correct calcuation result?
count = Convert.ToInt32(cmd.ExecuteScalar());
Label1.Text = count.ToString("###,##0");