|
...and? That's the expected behavior. Form_Load fires before the form is rendered on screen. Are you perhaps looking for the Form_Shown event?
|
|
|
|
|
Dave,
Looking at the code you will see that the message box is not in the form load event, but in a button click event. The button is on the form. I have lots of projects like this one and have never before encountered this behavior.
Bobby
|
|
|
|
|
Your code doesn't make any sense. Is the button Click event and the Load event handles in the same form?
If so, what are you doing to call the Click handler without the form being visible?
Unless you've done something very f'ed up in your code it is not possible for THAT messagebox line to be executed when you say it is.
|
|
|
|
|
Dave,
Your question is exactly the one I am looking to answer. There is no call to the button click event. So, I wonder where the call might be coming from. When I remove the message box from the button click event everything works as it should and the form loads.
Bobby
|
|
|
|
|
We can't see your code so it's up to you to find the problem.
I'd start by putting a breakpoint in the top of the button Click event handler and running the code. Look at the Call Stack to see where the call came from.
|
|
|
|
|
Dave,
The call is from a radio button, but I have no idea where the call originates. It must be buried somewhere within the VS generated code. So I will give up and simply remove the message box. Thanks for your advice.
Bobby
|
|
|
|
|
Yeah I seriously doubt it's in the designer generated code. It won't reference anything in your event handlers, ever.
This is a sign you've got something really messed up in your code and the stack trace is your best bet for finding it. I'd take a careful look at every method listed in the stack trace.
|
|
|
|
|
BobbyStrain wrote: The call is from a radio button Whereabouts on the form is this button, and does it get initialised somewhere? You should go back to the designer and check all the properties and events of the button, it is obviously firing for some reason. And, as Dave K. says, breakpoints and stack traces should find the issue fairly quickly.
|
|
|
|
|
Richard,
I have replaced the radio buttons with checkboxes. The checkboxes are not linked, but they are located within a group box. I modified things such that the checkbox is disabled and unchecked initially. It is activated and checked at the end of the form load code. I don't get the message anymore because the form load fills in all the input data. The checkbox checked change event is still firing on form load which fires the calculation, too. I don't have a clue as to how to run a stack trace, but I guess I will need to learn. We have come a long way since I started using Microsoft basic in 1982 with an 8-bid Z-80 4 mh processor. I have lots of small applications using VB.net and this is the first time I have encountered such mysterious behavior. It probably is related to upgrading with VS 2010. Thanks for your help. And you, too, Dave.
Bobby
|
|
|
|
|
BobbyStrain wrote: I don't have a clue as to how to run a stack trace, but I guess I will need to learn. Set a breakpoint in the source code window (click in the margin by the first line of code) of Visual Studio and press F5 to run the program in the debugger. When it stops at the debug point you can single step through the code, line by line, and inspect and change the variables, so you can see exactly what is happening. The debugger is probably the most useful tool at your disposal, so you should really get familiar with it. MSDN has some help information about it at https://msdn.microsoft.com/en-us/library/ms172744.aspx[^].
|
|
|
|
|
After further study, the checkbox change event is triggered during form load. So its handler is executed. I didn't realize that this is normal behavior. I already have a hidden label on the form that is used for another purpose, so I set the text during form load before enabling the checkbox, then skipped the message box display in the handler based on the value. I don't understand enough about senders and sender arguments, but this is probably the professional approach. So I learned a few new things. Thank you both.
Bobby
|
|
|
|
|
I'm trying to run the following query :
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp=2).Sum(Function(t3) t3.quantity), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp=2).Sum(Function(t3) t3.value) _
}).ToList
( in this query .quantity and .value have Decimal type.)
but I'm getting this error on runtime :
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
Additional information: The cast to value type 'System.Decimal' failed because the materialized value is null.
Either the result type's genericparameter or the query must use a nullable type.
It's sure that the collection child1 has items that have .tp=2.
What's wrong ? Thank you !
|
|
|
|
|
It's pretty simple really. It's telling you that, somewhere in your data, you have a null value in the database where this code is expecting a value. It can't convert a null to a Decimal type.
|
|
|
|
|
If I run the query like this :
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp=2).Select(Function(t3) t3.quantity), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp=2).Select(Function(t3) t3.value) _
}).ToList
Everything is ok. And it's sure that all the value on database are not null.
So the problem is the function Sum.
|
|
|
|
|
I already told you how to get the query string from the IQueryable object.
Get and execute it in SQL Server Manager. This is an INVALUABLE technique for diagnosing problems in your LINQ query.
I think you'll find the the Sum operation isn't done by LINQ, but by the database engine. You'll also find that the SQL that is generated isn't what you think it is.
modified 16-Feb-15 8:58am.
|
|
|
|
|
It's strange because if I replace the Sum function with Count function , everything works ok , and count values are not Null.
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp=2).Count(Function(t3) t3.quantity), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp=2).Count(Function(t3) t3.value) _
}).ToList
|
|
|
|
|
If you try to Count a null collection, it works because there's nothing to Count.
If you try to Sum a null collection, 0 + null isn't defined, so it throws.
|
|
|
|
|
That's the the great thing about LINQ: you can write the most complex operations in a single statement so when it fails you have no idea where the problem is.
|
|
|
|
|
These are the tables :
MyObj1:
Id name
2 name1
7 name7
8 name8
Child1:
ID ParentID TP Quantity Value
1 2 2 7 9
2 7 2 20 10
3 7 2 8 11
( ParentID is the forign key for child1 related to ID field on MyObj )
So the problem is on my query structure or in the tables ?
|
|
|
|
|
Based on that data, you don't have any Child1 rows for the MyObj1 row with ID 8. Since that row is included in your outer query (where t1.id > 6 ), you're trying to take the Sum of an empty sequence.
It seems that the EF query is returning NULL from the database, and then trying to convert that to a Decimal value. This issue is discussed on Matteo Tontini's Blog[^], albeit for C#.
Try casting the properties to Decimal? / Nullable(Of Decimal) before summing them.
Nullable Value Types (Visual Basic) - MSDN[^]
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp = 2).Sum(Function(t3) DirectCast(t3.quantity, Decimal?)), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp = 2).Sum(Function(t3) DirectCast(t3.value, Decimal?)) _
}).ToList
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I try your code , but now I get an error on design time : Decimal Value cannot be converted to Decimal?
|
|
|
|
|
That's annoying - C# just uses (decimal?)t.value , but it seems VB.NET can't cope with that.
You can either use the constructor:
Dim lst = (From t In context.MyObj1 where t1.id > 6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp = 2).Sum(Function(t3) New Decimal?(t3.quantity)), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp = 2).Sum(Function(t3) New Decimal?(t3.value)) _
}).ToList
Or explicitly specify the type argument for the Sum method:
Dim lst = (From t In context.MyObj1 where t1.id > 6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp = 2).Sum(Of Decimal?)(Function(t3) t3.quantity), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp = 2).Sum(Of Decimal?)(Function(t3) t3.value) _
}).ToList
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I try both your methods :
The first produce this error on runtime :
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
Additional information: Only parameterless constructors and initializers are supported in LINQ to Entities.
The second method, produce this error on design time :
Error 103 Overload resolution failed because no accessible 'Sum' accepts this number of type arguments.
|
|
|
|
|
Sorry, I forgot you need to specify the source type parameter as well. Something like:
t.child1.AsQueryable.Where(Function(t2) t2.tp = 2).Sum(Of Child1, Decimal?)(Function(t3) t3.quantity)
where Child1 is the name of your class.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello !
I'm using this expression :
t.child1.AsQueryable.Where(Function(t2) t2.tp = 2).Sum(Function(t3) CType(t3.quantity, System.Nullable(Of Decimal)))
now I'm not getting any error message , but I have another problem :
As you have noted on your previous posts , the item on MyObj1 with id=8 has no child1 items. But for this item I need that the sums to return 0 in the query , but instead I have no value at all for this item. what should I do ?
Thank you !
|
|
|
|