Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use this code for searching data date wise.

I want to search all the records in gridview month wise.
C#
DateTime date = Convert.ToDateTime(TextBox1.Text);
SqlCommand cmd = new SqlCommand("SELECT * FROM Tech_data where Call_assign = @Call_assign and dateadd(dd, datediff(dd,0, [date_time]), 0) = @date_time  ");

cmd.Connection = con;
cmd.Parameters.Add("@Call_assign", SqlDbType.VarChar).Value = DropDownList1.SelectedValue;
cmd.Parameters.Add("@date_time", SqlDbType.DateTime).Value = date.Date;

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Tech_grid.DataSource = ds;
Tech_grid.DataBind();
Posted
v3
Comments
ZurdoDev 18-Mar-13 8:54am    
Can you be more specific with where you are stuck?
Shubham Choudhary 18-Mar-13 9:24am    
ok!! you use this code very good!!!
OriginalGriff 18-Mar-13 9:31am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
bbirajdar 18-Mar-13 10:11am    
Try this in your code- SELECT * FROM Tech_data where Call_assign = @Call_assign and DATEPART ( month , date_time ) = DATEPART ( month , @date_time ) AND DATEPART ( year , date_time ) = DATEPART ( year , @date_time )

1 solution

I have not been able to determine what you are trying to do with the clause: dateadd(dd, datediff(dd,0, [date_time]), 0). It just compares the date/time of the date_time column to the @date_time parameter while using a few extra processor cycles. Since DateTime DataTypes are accurate to fractions of a second, the chances are small that you will get a match.

The following selects those rows that are in the same month and year as the @date_time parameter. Is this your question?
SELECT * FROM Tech_data where Call_assign = @Call_assign and MONTH(date_time)=MONTH(@date_time) and YEAR(date_time)=YEAR(@date_time)


Tested: SQL Server 2012
 
Share this answer
 
v3
Comments
[no name] 18-Mar-13 10:51am    
Good one man

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