VB.Net is my issue with DataRow.IsNull or Format(datetimevalue,"T")? - vb.net

I am working on a new ASP.Net 4.0 data driven app. In the DAL I check for NULL values on all my data and default to the proper data when NULL. I have a strange issue going on. Two dates are coming back - on one I need the time only. The First line of code for the full date works without fail - but the second line of code errors pointing to the format string but the strange part is that it errors on NULL values which does not use the format string and just returns Date.MinValue. When the second line gets data it formats the return correctly.
Dim dr As DataRow
.TourDate = IIf(dr.IsNull("tourdate"), Date.MinValue, Format(dr("tourdate"), "MM/dd/yyyy"))
.TourTime = IIf(dr.IsNull("tourtime"), Date.MinValue, Format(dr("tourtime"), "T"))
The error comes on the second line when dr("tourtime") is NULL - the erroe is: Argument 'Expression' is not a valid value.

IIf in VB.Net does not do short-circuit evaluation, so the Format call is being executed even if the value is null.
You need to use If:
.TourTime = If(dr.IsNull("tourtime"), Date.MinValue, Format(dr("tourtime"), "T"))
This is the same issue described here: Using VB.NET IIF I get NullReferenceException

To trouble-shoot this, I would inspect the actual value stored. A datetime column that appears to be null may actually have a value.

I think you should use IsDbNull instead of IsNull.

Related

DateInput yields empty strings for allowEmpty inputs

One hiccup we ran into with React Admin was around date fields for a table. We're just using the stock ra-data-json-server package. Our backend should be receiving a null value for an empty date, but it comes through as a blank string instead. What's the best approach for handling this?
Creating a custom DateInput component that yields null for an empty date.
Creating a custom data provider that would convert an empty string to null (not sure if it would have enough context to do this, though).
Something else I haven't thought of.
I'm not keen on doing the translation at the API end, since I'd like to keep the API clean and only allow for a valid date or a null value.
You can transform the input value using the parse / format functions:
https://marmelab.com/react-admin/Inputs.html#transforming-input-value-tofrom-record

If - Strange Behavior in Detecting Type

I discovered this strange behavior in VB.Net today in trying to work with nullable DateTime data. I am pulling a DateTime value out of an XML file for inserting into a database, and I want to allow for an empty value. So I thought I should use If to prevent casting errors:
Dim LastRun As DateTime? = _
If(rowData("LastRun") = "", Nothing, CType(rowData("LastRun"), DateTime))
It seems like this should return a value of Nothing in the case that the If is false, or a value of the date time from LastRun if the value is not blank. Instead, when the If condition returns false, I get a value of DateTime.MinValue, which causes an exception on insert to the database due to SQL DateTime underflow.
I was able to fix it by using DateTime? as the cast in the last parameter, but this behavior seems odd to me. The expected type is clearly DateTime? because that's the variable type. Also, the narrowest type that can allow for both possible result values is DateTime?, since it could be either a DateTime or Nothing. And yet somehow it decides that the result value should be DateTime and then I guess typecasts Nothing to DateTime.MinValue? What is going on here?
Part of the problem is I'm used to C#, and the equivalent expression rowData["LastRun"] == "" ? null : (DateTime)rowData["LastRun"]) doesn't even compile (as expected), because there's "no implicit conversion between DateTime and null."
Nothing is not the same as null in C#, it is a mixture between null and default(T). So when you use Nothing on a value type(like the structure DateTime) you get it's default value what is DateTime.MinValue.

Convert String Variable to Datetime Returns Incorrect Value in SSIS

Following my question in https://stackoverflow.com/questions/7387418/where-clause-using-date-taken-from-string-variable-sent-out-incorrect-return-in-s, this is the simplified version of the question:
The query used for the OLE DB Source is this, with a string type variable inside:
select
*
from
A
where
A.A_IN_DATETIME < CONVERT(DATETIME,?,105) and
(A.A_OUT_DATETIME is NULL or A.A_OUT_DATETIME >= CONVERT(DATETIME,?,105))
The query above works inside a Foreach Variable Enumerator, with the variable used is an array of string variable consisting of this: 13-09-2011,12-09-2011,11-09-2011,10-09-2011,09-09-2011,08-09-2011,07-09-2011,06-09-2011,05-09-2011,04-09-2011,03-09-2011,02-09-2011,01-09-2011
The condition for the problem is this: For example, there is a record in A with A_IN_DATETIME = 2011-09-12 (YYYY-MM-DD format) and A_OUT_DATETIME = NULL.
The correct result for the query above that it should have been only returning values for 13-09-2011 and the rest return 0 records, but in the execution, there was a result for 12-09-2011 to 10-09-2011 also. I don't know if the SSIS somehow mistaken the 12-09-2011, 11-09-2011, 10-09-2011 to 09-12-2011,09-11-2011,09-10-2011 (FYI, I've checked the parsing result and also the loop enumerator by printing it out in a message box generated from the script task, and it is still in its correct form).
Is there any solution for this problem?
Run Sql Profiler and see what query comes through to SQL Server. This will allow you to re-run the exact query in SSMS and see the results right there.
I don't believe conversion to DATETIME accepts a format, i.e. 105.Try this instead:
CONVERT(DATETIME, '9/13/2011 15:37:00')
Result is DATETIME - 2011-09-13 15:37:00.000

Reporting Services - handling an empty date?

Hey, I have a report parameter which looks like this: 01.01.2009 00:00:00
Its a date (as string), as you might have guessed :). The problem is, this param can be an empty string as well. So I tried those expressions:
=IIf(IsDate(Parameters!DateTo.Value), CDate(Parameters!DateTo.Value), "")
=IIf(Len(Parameters!DateTo.Value) > 0, CDate(Parameters!DateTo.Value), "")
Both dont work and the value for the textfield where I print the expressions result is always #Error. As soon as I remove the CDate stuff, it works, but I have to use it. IS there another way to achieve that? What I want is to display nothing if its not a date or the date (format dd.mm.yyyy) if its a date.
Ideas?
Thanks :)
All arguments to the IIf are evaluated, which results in your error, since the CDate will fail for an empty string.
You can get around this by just writting a function along these lines, using a standard if statement:
Function FormatDate(ByVal s As String) As String
If (s <> "") Then
Return CDate(s).ToString()
Else
Return ""
End If
End Function
Then call it with: =Code.FormatDate(Parameters!DateTo.Value)
First, fix your database to properly store dates rather than doing these workarounds. You probably have bad data in there as well (Feb 30 2010 for example or my favorite, ASAP). Truly there is no excuse for not fixing this at the database level where it needs to be fixed except if this is vendor provided software that you can't change (I would yell at them though, well notify them really, and ask them to fix their data model or go to a new product designed by someone who knows what they are doing. A vendor who can't use dates properly is likely to have software that is very poor all around).
In the query that you use to select the infomation, have you considered just converting all non-dates to null?

InvalidCastException when parsing dates in VB.NET

I'm trying to Parse dates entered into a TextBox into a DateTime value but I keep getting an exception in the TryParseExact method. What I'm trying to do is:
DateTime.TryParseExact(tbAddDate.Text.Trim, "yyMMdd", New CultureInfo("sv-SE"), DateTimeStyles.None, row.Date)
This throws an InvalidCastException with the message "Conversion from type 'DBNull' to type 'Date' is not valid." I realize what is happening is that it's trying to set row.Date to DBNull which is not a valid value for a DateTime. What I don't understand is why it's trying to do this, as the documentation states that it should be set to MinValue and not DBNull.
As a sidenote, I know that I can get around a lot of these problems by using a DateTimePicker but the customer feels that they are very clunky as it's not possible to enter the dates directly with the keyboard.
It seems that (as magnifico's comment suggests) the problem is strictly related to the fact you're passing row.Date as the result argument. It must occur when the code attempts to assign row.Date with Date.MinValue (unless the documentation is inaccurate, which is less likely in this scenario).
I'd advise that you pass some different result argument, and use its value to update row.Date after calling TryParseExact. It might not be the ultimate permanent solution, but it should help you inspect this bug and find the cause to the InvalidCast exception.
Sidenote: perhaps this row.Date doesn't accept Date.MinValue as a legitimate value, i.e.: row.Date might be willing to accept only dates from 01/01/2000 onwards, and Date.MinValue returns 01/01/0001.