Click here to Skip to main content
13,195,564 members (61,492 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello..... I've created a database in which i have saved some data along with date. Now i want to extract data from database of a particular month by selecting Calender and to display data in gridview in Please solve my problem. My code is as of follows:
private SqlConnection cn = new SqlConnection("User Id=sa;Password=12345;Database=Satyam;Server=CHIKLU-PC");
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
            SqlCommand cm = new SqlCommand("SELECT * FROM empreport WHERE ename='" + TextBox1.Text + "' and indate='" +Calender1.SelectedDate.Month.ToShortDateString()+ "'", cn);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            GridView1.DataSource = ds;
        catch (Exception ex)
Posted 6-Feb-13 5:43am
Updated 6-Feb-13 5:47am
richcb 6-Feb-13 11:48am
Are you receiving any exceptions? Where does the code stop when you are debugging?
Chiklu.Soumya 6-Feb-13 11:55am
It doesn't showing any exception. It's working only when a specific date is selected.(Calender1.SelectedDate.ToShortDateString)i'm using this code. But I want it in month and don't know how to do it.
richcb 6-Feb-13 12:08pm
I would just use "Calendar1.SelectedDate.Month.ToString()". Try that and see what happens.
Chiklu.Soumya 6-Feb-13 12:26pm
No. Error. But gridview doesn't showing any data.
richcb 6-Feb-13 12:42pm
If you are not getting any error, than your query is not returning anything. What datatype is the field "indate" in your query?
Chiklu.Soumya 6-Feb-13 12:56pm
Chiklu.Soumya 6-Feb-13 13:23pm
Is it possible to extract data from a table of a particular month by selecting any date(of that month) in calender and to display them in a gridview(table doesn't have a month column and it has column of datetime where only dates are saved.).So is it possible to get month from those dates.
Ex: CREATE TABLE Sales(productname varchar(50),quantity int,saledate datetime)
Insert: Nokia,10,02/07/2011. Can I select month from the specified date from table?
Chiklu.Soumya 6-Feb-13 13:26pm
Is it possible to extract a month from specific date from datebase?
richcb 6-Feb-13 14:16pm
Yes, if I understand correctly. Change your query to this: "SELECT * FROM empreport WHERE ename='" + TextBox1.Text + "' and indate like '%" + Calender1.SelectedDate.Month.ToString() + "%'".

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to write query to get data for a given month. Try something like following:
   MONTH(myDateColumn) = @SelectedMonthParam

Refer: MSDN: MONTH (Transact-SQL)[^]
NOTE 1: Make sure datatypes match. It returns integer , so pass integer from frontend.
NOTE 2: MONTH returns the same value as DATEPART (month, date).

BTW, the way you have implemented ADO.NET is open for SQL Injection. Read about protecting from SQL Injection here: SQL Injection Mitigation: Using Parameterized Queries[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171019.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100