Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to filter the the data on the bases of start date and end date.. and on the bases of that filter i am performing addition(Sum) of price..

i have written the test code.. but its not working with start date and end date..

ignore concentination concatenation (Sql Injection) i just wrote this before implementing it on my project..

here is the test code.. what should i amend to make that code work.

here is the Code..

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection MAconn = new SqlConnection();
        MAconn.ConnectionString = connectionString;
        MAconn.Open();
        
        SqlCommand MAcmd = MAconn.CreateCommand();

        MAcmd.CommandText = "select Sum(price) from sale where startdate='"+TextBox1.Text+"' and startdate='"+TextBox2.Text+"'";
        lblsale.Text = System.Convert.ToString(MAcmd.ExecuteScalar());

        
        MAconn.Close();

    }
Posted
Updated 3-Dec-11 15:21pm
v2

No Doubt the code is perfect. but you are taking StartDate and EndDate as string. but if you see in sql Server you may have set its dataType to DateTime. so you have to convert this string to dateTime and then pass it in Query. instead of using textbox, i would prefer to use DateTimePicker.
 
Share this answer
 
v3
Comments
codegeekalpha 3-Dec-11 10:30am    
i am using string in sql server as well.. and i can't use date time picker in asp.net.. i am using ajax control tool kit..
Richard MacCutchan 3-Dec-11 10:49am    
If you are using string in SQL server then the only date calculations or comparisons you can do are for string equality. This is bad design and should be rectified as soon as possible; see my comments above.
codegeekalpha 5-Dec-11 4:29am    
5+
C#
MAcmd.CommandText = "select Sum(price) from sale where startdate='"+TextBox1.Text+"' and startdate='"+TextBox2.Text+"'";

startdate cannot be equal to both values, unless they are the same string; is that what you expect?

I would also guard against using text strings for date comparisons, you should always use DateTime values as they cannot be misinterpreted. Strings of the form "10/12/2011" are ambiguous; is that 10th of December or October 12th?
 
Share this answer
 
Comments
codegeekalpha 3-Dec-11 10:31am    
no this is the format which is used in my country.. 10/december/2011
Richard MacCutchan 3-Dec-11 10:45am    
And that is the whole point of my comment, strings vary from country to country; do not use strings for date values, always use DateTime objects.

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