navio legacy
navio legacy

Reputation: 31

I want to compare system date with my SQL stored database date using query in ASP.NET C#

i have stored data in SQL.there is a column "date" with 'DateTime' data type. now i want to compare system date with my database date using query. please help friends thanks in advance.

i have tried this code which are shown in its shows the total data of table. i want to show data current date to last 3 month of data of my tabel.(thanks)

What I have tried:

SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
  con.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
  con.Open();
  try
  {
    DateTime maxdate = DateTime.Now.Date;
    DateTime mindate = DateTime.Now.Date.AddMonths(-3);
    SqlDataSource1.SelectCommand = "select * from general1 where date<='" + maxdate + "' AND date>='" + mindate + "'" ; 

Upvotes: 1

Views: 86

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416081

Assuming you have a control bound to the SqlDataSource1 component from your sample code:

protected void Page_Load(object sender, EventArgs e)
{
    SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString
    SqlDataSource1.SelectCommand = "select * from general1 where date<= current_timestamp AND date>= dateadd(month, -3, current_timestamp)";
}

Upvotes: 1

Related Questions