How to compare date with string in Linq with MVC4? - c#

Hi I am developing one MVC$ application. I came across one query. In one of my database table I have datetime column. I will be storing data as below in that column.
Eg: 2016-10-05 11:57:54.650
I have one jquery calender where i will receive date as string. I will receive in the below format.
eg: 06/10/2016
I have below Linq query where I am comparing my jquery date with database date as below.
Below code I have tried so far.
string datemodified;
DateTime dt = Convert.ToDateTime(datemodified);
upld_id = (from c in db.upldTable where c.upld_clientid==clientId && EntityFunctions.TruncateTime(c.upld_ModifiedDateTime) == dt select c.upld_docid).ToArray();
My query does not work fine. My query not returning expected result. I am having problem in comparing date portion. If i did not compare date then my query works as expected. So I am sure my date comparision is not happening above. Can someone help me in this regard? Thanks a lot.

Related

DateTime.Date conversion in EF Core Generated Query

We have a project we are working on and we need to query the database for some data for a particular date.
We configured our DB to read and write date as UTC.
When writing the query to get the data, I noticed that the data for a date was not being pulled from the database.
Here is the code:
transactionDate = Convert.ToDateTime("2021-11-10:T10:00:00").ToLocalTime();
var transactions2 = _transactionsRepo.Query()
.Where(transaction => transaction.AccountId == pharmacy.AccountId.Value)
.Where(transaction => transaction.TransactionDate.Date == transactionDate.Date)
.OrderByDescending(transaction => transaction.TransactionDate)
.Skip(numToSkip)
.Take(pageSize);
On investigation, I noticed that when pulling the data, the DB returns the date as UTC as it should and the date is compared to the input date. But no data is returned. I checked the query generated and noticed this:
DECLARE #__transactionDate_1 datetime = '2021-11-10T10:00:00.000';
DECLARE #__p_2 int = 0;
DECLARE #__p_3 int = 10;
SELECT *
FROM [WalletTransactions] AS [w]
WHERE ([w].[AccountId] = #__AccountId_Value_0) AND (CONVERT(date, [w].[TransactionDate]) = #__transactionDate_1)
ORDER BY [w].[TransactionDate] DESC
OFFSET #__p_2 ROWS FETCH NEXT #__p_3 ROWS ONLY
From the above, the query generated shows that the TransactionDate is converted to just Date and compared to the input date #__transactionDate_1 which is in DateTime form.
Any help on how to solve this will be deeply appreciated.
For anyone facing the same issue, here is a link to the resolution on EF core repo on github:
https://github.com/dotnet/efcore/issues/28380
The issue was caused by a custom value converter that saves and reads all date as UTC.
This means a double conversion was happening and skewing the date time.
Solution is to specify that the input date is in UTC and hence should not be converted.

Way to load data between dates in report viewer in C#

I am trying to get data between two dates in a report viewer control in windows forms. Filtering report data, so I modified the dataset using parameters to
select SN,invoice_date,product_code........ where invoice_date >= #date1 and invoice_date <= #date2
I tried this also
select SN,invoice_date,product_code........ where invoice_date between #date1 and #date2
But the query is not returing any data, the datatype I used in SQL Server is date, and I changed the properties of the dataset #date1 and #date2 into date. So the problem is the datetimepicker am using is not working as it is adding time to the value. And I tried to validate the datetimepicker into something like
datetimepicker1.value.date.toshortdatestring() even tried datetimepicker1.value.tostring()
But the problem is, it will generate an error that system.datetime cannot be convert into string. I even change the datatype of the #date1 and #date2 into varchar in the dataset properties but still not loading.
So this is the code tried
this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value, ProductTo.Value, txtproductcode.Text);
this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value.Date, ProductTo.Value.Date, txtproductcode.Text);
What am I missing? I am using C# with SQL Server.
Assuming that the signature of Sales_InvoiceTableAdapter.FillByget is FillByget(string invoice, DateTime from, DateTime to, string productCode), then you should not need to convert to a string and you should check you're setting the time portion appropriately.
this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value.Date, ProductTo.Value.Date.AddDays(1), txtproductcode.Text);
Given a ProductFrom.Value and ProductTo.Value equal to 2020-04-28T10:55:00, the above code will result in ProductFrom.Value = 2020-04-28T00:00:00and ProductTo.Value = 2020-04-29T00:00:00.
If you want to only get values on that specific day (excluding midnight), then subtract 1 second from your final to value. e.g. ProductTo.Value.Date.AddDays(1).Subtract(TimeSpan.FromSeconds(1)).

Unable to select records between date range

In my Windows Form C# Application, I am inserting the record into the SQL server database. Along with other fields, I am also inserting both the current date and current time using the following format:
DateTime currentDate = DateTime.Now;
string SaleDate = currentDate.ToString("dd-MM-yyyy");
DateTime currentTime = DateTime.Now;
string SaleTime = currentTime.ToString("hh:mm:ss tt");
Which works fine and the records are inserted successfully with Date and Time format like .
However, when I select records between two dates, I am unable to perform this operation. I am sure that there is no problem in this custom format of my date because I have edited the same format several times and even I have saved the date in its default format but still I am unable to select the target records (between two dates)
I am using the following select query in my application to select records from the view vAllSales which results in selecting either all the records or records whose SaleDate does not meet the specified filter criteria:
select * from vAllSales where SaleDate >= '20-04-2019' and SaleDate <= '30-04-2019'"
I have tried the following queries in my SQL server as well to inspect the cause:
1.
select * from vAllSales where SaleDate between '21-04-2019' and '09-05-2019'
The above query does not return any value. Like
2.
select * from vAllSales where SaleDate >= '21-04-2019'
The above query select only a few records. Because 21-04-2019 is the initial SaleDate and the query should return all of the Sales but it only returns a few records, like .
Is there any issue with the specified date format within my code? Or do I have to work on improving my queries? I need a more professional and reliable approach for this case.
You should store your date and time as datetime2 objects in your database.
It will give you the benefits of:
better sorting: (your current string format does not allow a natural sort)
better performance; since your date and times are represented as numbers in stead of strings
easier to handle in code and queries: since it maps directly to a C# DateTime object.
For more info on database types see:
https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime2-transact-sql?view=sql-server-2017
And perhaps:
https://learn.microsoft.com/en-us/sql/t-sql/data-types/time-transact-sql?view=sql-server-2017
If you absolutely must use a string to represent your date, make sure it's in the sort-able ISO format.
YYYY-MM-DD
due to the nature of this format it's lexicographical sort-able as a string type.
You are trying to apply logical operations to string values. What will here happen is lexicographical comparing. (left to right).
So you better use correct data types to store data in table.
But If you really want to store date as a string, you have to cast them to proper data types before comparison.
select * from vAllSales where CAST(SaleDate AS DATE) >= '21-04-2019'
Keep note that ms sql server uses single quotes for pass datetimes. That doesn't mean those are strings or varchar

asp.NET and SQL Server datetime issues

I am having hard time to store date information into the datetime column of SQL Server.
I get the input from the user for three columns:
Creation Date
Preparation Date
Next Preparation Date
I use calendarextender and format the date as "yyyy/MM/dd". When all the fields have date, they are stored in the DB as for instance, 16-10-2016 (dd-MM-yyyy).
At this point I have two issues:
These columns are optional, when some of them are empty my code does not work (I assume because datetime cannot be null). To overcome this, I am using the following code snippet but still does not work.
DateTime? creationDate= null;
if (creationDateTextbox.Text != null && creationDateTextbox.Text != "")
{
creationDate= Convert.ToDateTime(creationDateTextbox.Text);
}
When I fetch the dates from DB, they are shown as 10/16/2016 (MM-dd-yyyy) which is different how I formatted it. I would like to show it in the format user enters them.
Dates do not have a format while stored in a database. It is actually usually just a very large long that counts the number of milliseconds from a set starting date.
If you want to store the format you need to stop storing it as dates and instead just treat the text as text in the database, however if you do this you won't get the advantage of sorting or filtering by a date range because it will just be seen as text.
Date time doesn't have any format You can format is as a string, suppose your DateTime type database field dt which contain date as 10/16/2016 (MM-dd-yyyy) then you can convert it
string s = dt.ToString("yyyy/MM/dd");
The answer to one of your questions is here: MSDN
You can use data annotations to format the dates that you get from your SQL DB. I'm assuming that you're using EF6; if not, you can change the field to a varchar in SSMS, and store the date as a String.
And the second, I'm unclear about, but if what you want is for your SQL DB column to be optional, you can use the Optional data annotation for that.

To Get Data that its type is Date in C#

In my SQL database, I have BeginDate column of type date (so I want to get only day,month,year).
I add data into database in C#.
I look at Table_Data and everything is fine (for example BeginDate = 05/04/2014).
But when I get this data from database in C# with
string a= dt.Rows[i]["BeginDate"].ToString()
result is: a= 05/04/2014 12:00:00 AM
Why?
I can remove hour in string a with any methods. But why? I want to get only 05/04/2014 from database.
This is a display problem, not a data problem. Use the Formatting features of DateTime.ToString() to display the date in the format that you want.
If the data is coming out of the database in string form, use DateTime.Parse on it first.
Reference
Standard Date and Time Formats
Custom Date and Time Formats

Categories

Resources