Reputation: 3736
I have an ASP.NET application that crashes when a null date value is returned.
The code line is:
excelWorksheet.Cells("A" & xlRowCounter).Value = IIf(IsDBNull(dRow("sysdate")), "", Format(dRow("sysdate"), "MM/dd/yyyy"))
Error message:
ERROR: System.ArgumentException: Argument 'Expression' is not a valid value.
How would I check for a null date, and replace it with "" in my app ?
Upvotes: 0
Views: 19994
Reputation: 111
i'm have same problem but i'm using vb.net. If Get value from excel worksheet with oledb, check first the cell format to take its value. In this case format is date in excel worksheet, and the way provided by Chris Dunway work done, thx dude.
Upvotes: 0
Reputation: 2713
Use a proper If statement or the new if operator. The if operator has exactly the same syntax as te old IIF function but only evaluates the arguments it needs to. The IIF function always evaluates ALL of its arguments. So even if dRow("sysdate") is null, the last argument to the IIF function is evalulated and causes the error.
Upvotes: 2
Reputation: 11216
You can also use a Nullable DateTime:
Dim sysdate As DateTime? = dRow.Field(Of DateTime?)("sysdate")
Dim result As String = If(sysdate.HasValue, sysdate.Value.ToString("MM/dd/yyyy"), "")
I presume that dRow is a DataRow from a DataTable
Upvotes: 0
Reputation: 46047
Try using If([condition], [true-part], [false-part])
instead:
If(IsDBNull(dRow("sysdate")), "", Format(dRow("sysdate"), "MM/dd/yyyy"))
If you need an extra check to test for nothing, you can do this:
If(dRow("sysdate") Is Nothing Or IsDBNull(dRow("sysdate")), "", String.Format("{0:MM/dd/yyyy}", dRow("sysdate")))
Upvotes: 3
Reputation: 2706
the String class has a IsNullOrEmpty Method. You can just check the cell with it first, if its true fill it with an empty string.
Upvotes: 0