Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 asp.net. 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)
    {
        cn.Open();
        try
        {
            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();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }
        cn.Close();
Posted 6-Feb-13 6:43am
Edited 6-Feb-13 6:47am
v2
Comments
richcb at 6-Feb-13 11:48am
   
Are you receiving any exceptions? Where does the code stop when you are debugging?
Chiklu.Soumya at 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 at 6-Feb-13 12:08pm
   
I would just use "Calendar1.SelectedDate.Month.ToString()". Try that and see what happens.
Chiklu.Soumya at 6-Feb-13 12:26pm
   
No. Error. But gridview doesn't showing any data.
richcb at 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 at 6-Feb-13 12:56pm
   
datetime
Chiklu.Soumya at 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 at 6-Feb-13 13:26pm
   
Is it possible to extract a month from specific date from datebase?
richcb at 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
good
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:
SELECT 
   * 
FROM
   empreport
WHERE
   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[^]
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 95
4 Maciej Los 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
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