how to search date within range in c Sharp dot net and mssql

On June 21

how to search date within range in c Sharp dot net and mssql

There are lots of time when it happens that you tried to search data within the date range
but you face issue such as data dont come out of certain time range, some time daata comes which is not even required,
here is the solution which you can use to get the perfect result for searching data perfectly within a range.

First at the code behind file in c# you need to format your date as follows.


String fromdata = DateTime.ParseExact(txtfromdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

String todate = DateTime.ParseExact(txttodate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);


This will resolve problems such as datetime not in correct format or invalid date format error given by sql.

now in SQL query do the following thind for selecting date within range


select * from table_name where createdate>=@fromdate and createdate<=convert(datetime, @todate + '23:59:59')



Yes, really goodone.

Could have been better.



Leave your comment