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
searchsanjaysharmaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

txtFromDate.text.ToString('d') should do the trick.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

searchsanjaysharmaAuthor Commented:
ok
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.