Click here to Skip to main content
15,901,368 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to retrieve the particular datas from sql database by clicking one date of calender control.
In my database i have taken name nvarchar(50),date nvarchar(50),year nvarchar(50) in the table name record.
Build is succeeded but it is not retrieving the datas from database.

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select  *from record where date='" + Calendar1.SelectedDate.Day.ToString() + "' and Month='" + Calendar1.SelectedDate.Year.ToString() + "'", con);
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.Fill(ds, "record");
        GridView1.DataSource = ds.Tables["record"].DefaultView;
        GridView1.DataBind();
        con.Close();
    }


Sir please give the solution.
Posted
Updated 28-Mar-11 3:25am
v2

As pointed out you're comparing month against year and so on. Also two hints for using the database:
- use proper datatypes in the datanase. Don't split the date to varchar columns, use date type in the table instead.
- use parameters in your queries. For more info see: SqlParameter[^].
 
Share this answer
 
Comments
Banajyotsna 30-Mar-11 6:14am    
Thank you sir for giving suggestion.Now it is working.
SqlCommand cmd = new SqlCommand("select *from record where date='" + Calendar1.SelectedDate.Day.ToString() + "' and Month='" + Calendar1.SelectedDate.Year.ToString() + "'", con);

I don't think it will ever work. You are looking for date which is equal to a DAY and month which is equal to a Year.
 
Share this answer
 
Comments
willempipi 28-Mar-11 9:44am    
http://msdn.microsoft.com/en-us/library/ms186313.aspx

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