• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

How do i compare dates in asp.net using C#.

I have date in dd-mmm-yyyy format in asp textbox
The backend is having the date in yyyy-mm-dd hh:mm:ss format. How do i compare them.
Ex. suppose i am taking txtfromdate.Text and txttodate.Text from the front end.
The backend contains startdate as field name in Datetime
I want to fetch records which lies between txtfromdate.Text and txttodate.Text
0
searchsanjaysharma
Asked:
searchsanjaysharma
3 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

txtFromDate.text.ToString('d') should do the trick.
0
 
baskar_ramCommented:
Hi,

Check the below code.. hope it helps!
using System;
using System.Data.SqlClient;
using System.Data;

public class Sample1
{	
	public static void Main(String [] args)
	{
		SqlConnection con;
		SqlCommand cmd;		
		
		string sDate1="";
		string sDate2="";
		DateTime dateTime1;
		DateTime dateTime2;
		
		try
		{		
			//sDate1 ="dd-mmm-yyyy ";
			sDate1 ="01-Mar-2010";
			sDate2 ="30-Mar-2010";
			
			dateTime1 = DateTime.Parse(sDate1);			
			dateTime2 = DateTime.Parse(sDate2);
			
			con = GetConnection();
			con.Open();
			
			cmd = con.CreateCommand();						
			cmd.CommandText = "Select name from datetest where startdate between @dateTime1 and @dateTime2";					
			
			cmd.Parameters.Add("@dateTime1", SqlDbType.DateTime).Value = dateTime1;
			cmd.Parameters.Add("@dateTime2", SqlDbType.DateTime).Value = dateTime2;
			
			SqlDataReader reader = cmd.ExecuteReader();			
			
			while(reader.Read())
			{
				Console.WriteLine("Name="+reader.GetString(0));
			}
			reader.Close();
		}
		catch(Exception ex)
		{	
			Console.WriteLine(ex);
		}
		finally
		{
			con=null;
		}		
	}		

	public static SqlConnection GetConnection()
	{
		SqlConnection con = null;
		string sServer = "";
		string sPort = "";
		string sUser = "";
		string sPwd = "";
		string sDatabase = "";
		string sUrl = "";							


		try
		{				
			sUser = "sa";
			sPwd = "sa";
			sServer = "server1";
			sDatabase = "testdb";

			sPort = "1433";

			//Connection string
			sUrl = "Server=" +sServer+ "," +sPort+ ";Database="+ sDatabase +";User ID=" + sUser + ";Password=" + sPwd;		

			con = new SqlConnection(sUrl);						
		}
		
		catch(Exception ex)
		{	
			Console.WriteLine(ex);
		}		

		return con;			
	}		
}
Sample Table data..

table :datetest

name	startdate
======================
Baskar	2010-03-31 14:44:59.587
Ram	2010-03-11 14:44:59.603
Raj	2010-04-02 14:44:59.610

Open in new window

0
 
searchsanjaysharmaAuthor Commented:
ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now