How do i compare dates in 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
Who is Participating?

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

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:

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

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;
			//sDate1 ="dd-mmm-yyyy ";
			sDate1 ="01-Mar-2010";
			sDate2 ="30-Mar-2010";
			dateTime1 = DateTime.Parse(sDate1);			
			dateTime2 = DateTime.Parse(sDate2);
			con = GetConnection();
			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();			
		catch(Exception ex)

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

			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)

		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:
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.