Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to sum column values between two dates.
the two date values are coming from asp calender.
how to write sql query and display result in textbox.
for eexample:
VB
query="select sum(column names) from tablename where date between calender1 and calender2"
Posted
Updated 28-Feb-15 6:50am
v2
Comments
[no name] 28-Feb-15 8:10am    
And now please explain where is the problem?
batman56 28-Feb-15 8:12am    
calender date format and date format in database table is not matching.
i using date as datatype in my DB.
[no name] 28-Feb-15 8:18am    
ok, this is out of my scope but I think you will get an answer soon here.
Bruno
batman56 28-Feb-15 8:20am    
ok.
thank you.

1 solution

Just pass the Calendar1.SelectedDate which is a DateTime value to the sql query, but use parameterized query to prevent Preventing SQL Injection Attack ASP.NET - Part I[^]
The code snippet:
DateTime startDate = Calendar1.SelectedDate;
DateTime endtDate = Calendar2.SelectedDate;
string commandText = "select sum(column) from tablename where datecolumn between @startDate and @enddate";
using (SqlConnection connection = new SqlConnection(connectionString))
{
	SqlCommand command = new SqlCommand(commandText, connection);
	command.Parameters.AddWithValue("@startDate", startDate);
	command.Parameters.AddWithValue("@endDate", endDate);
        Int32 sum = (Int32) command.ExecuteScalar();
        Textbox1.Text = sum.ToString();
}

Refer:
1. Calendar.SelectedDate Property[^]
2. SqlCommand.Parameters Property[^]
 
Share this answer
 
v6
Comments
batman56 28-Feb-15 9:17am    
thank you sir.
please tell me how can i display the sum in the textbox,i am using asp.net.
Peter Leow 28-Feb-15 9:29am    
Added. I drafted the code snippet without testing. You have to adapt it to your situation.
batman56 28-Feb-15 9:34am    
it worked.
thank you sir.
Maciej Los 28-Feb-15 12:50pm    
+5
Peter Leow 28-Feb-15 20:13pm    
Thank you, Maciej.

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