Typed DataSet - InvalidCastException - vb.net

I've got a typed dataset. I bind the data with a binding source to a form with arround 200 textboxes, dataedits and so on. Everything works fine with one exception:
In my database I have empty DateTimes (DBNull.Value). And when they get bound to a DateEdit-Control, I get the following exceptions:
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.Data.StrongTypingException' occurred in myTestDLL.dll
I tried to change the NullValue-Property in the DataSet-Desinger to something else as 'Throw Exception', but it doesn't work for a DateTime. For other types like Integer or String it works fine.
I dont know a nice solution (right now I fill the empty dates with some dummy date and make it invisible in the DateEdit-Control, but this is very uncool) and I hope to find some help here.
It seems that many people got the same problem but I didn't find a solution :(

Why not just skip if test fail ?
if dMyDate is dbnull.value then 'skip, msgbox, set to date.minValue, whatever

Related

System.InvalidCastException: Unable to cast object of type 'SelectedObjectCollection' to type ,System.Collections.Generic.lEnumerable'1[System.String]

System.InvalidCastException: Unable to cast object of type 'SelectedObjectCollection' to type ,System.Collections.Generic.lEnumerable'1[System.String]
I need help understanding this runtime error. I am fairly sure that the below is the problem, but have not been able to find a solution that works.
Parallel.ForEach(Of String)(Me.clb1.SelectedItems,
Sub(clb1)
Parallel.ForEach(of String)(Me.CheckedListBox1.SelectedItems,
Sub(xxx) <-Not sure what goes here.
The error message highlights the entire Sub but the rest was cut and pasted from working code.
I read the instructions of Lambda and found a simpler example. The is dawned on me what I needed to do.
For cnt = 0 To ListBox1.Items.Count - 1
MyFiles.Add(ListBox1.Items(cnt))
Next
Parallel.ForEach(MyFiles, Sub(F)
Now I need to fix the code to handle the multi-threading code, but that is another question that I am not ready to ask yet. Thanks.

What are the correct syntaxes for referring to a subform, and why is the recommended one producing an error?

I have the following two lines of code:
Debug.Print Forms!DocLoader!DL_RowBox!DLR_FileName.Name
Debug.Print Forms!DocLoader!DL_RowBox.Form!DLR_FileName.Name
The second one, which I have seen recommended in almost every VBA reference, including the answer being suggested from SO as I type this, follows this structure:
Debug.Print Forms![Form Name]![Subform Control Name].Form![Control Name].Name
These two lines of code should produce the same result. However, the second, recommended syntax throws error 40036, "Application-defined or object-defined error" unless I am in design view. I cannot use it at runtime, but I have never seen this limitation mentioned in any of the reference documentation or forum posts I have looked at. The first line, using only default parameters, seems to work no matter the context.
With the second line, I have tried just about every combination of bang and period I can, and I have also tried enclosing field names in brackets, but the common denominator is that as soon as I reference ".Form" the application throws an error. Even something simple like ".Form.Caption" has thrown an error. So what I would like to know is:
Are there any other correct ways of referring to a subform's form properties, since I need these as well as its controls
Why would the first line execute correctly while the second, recommended one does not seem to work?
Running the compiler appears to have fixed the issue.

What does "<8>" mean as a value?

I'm getting an exception report from a vb.net Windows Forms app I made via the exception reporting system I built into it. Some users are getting an exception with message "Value was either too large or too small for a Decimal.Couldn't store <8> in PrdHrs Column. Expected type is Decimal."
The stack trace included tells me the method, and that message narrows down where the exception is being hit, but I can't figure out what "<8>" means. Does that just mean the value of 8, but it's displayed with less than/greater than symbols around it? Or does that imply a certain value type? Google is basically impossible to search for this.
I suspect that that '8' actually is an infinity symbol but it's not displayed properly in whatever you're seeing. I just tried this code:
Dim dbl = Double.PositiveInfinity
Dim table As New DataTable
table.Columns.Add("Number", GetType(Decimal))
table.Rows.Add(dbl)
and this was the message I got:
System.ArgumentException HResult=0x80070057 Message=Value was either too large or too small for a Decimal.Couldn't store <∞> in
Number Column. Expected type is Decimal. Source=System.Data
StackTrace: at System.Data.DataColumn.set_Item(Int32 record, Object
value) at System.Data.DataTable.NewRecordFromArray(Object[] value)
at System.Data.DataRowCollection.Add(Object[] values) at
ConsoleApp1.Module1.Main() in C:\Users\johnm\AppData\Local\Temporary
Projects\ConsoleApp1\Module1.vb:line 12
Inner Exception 1: OverflowException: Value was either too large or
too small for a Decimal.
It's not ideal that you are working with Double values and then storing the results as Decimal values. It may be unavoidable but, if so, you need to do a bit better at validation. You are probably dividing by zero, which is legal for Double values and produces either Double.PositiveInfinity or Double.NegativeInfinity.

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

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.

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.