Why can my VB.NET User Control not instantiate an ObjectContext? - vb.net

I have the following lines of code as a test user control. When the project is built, and I drag this user control onto a form, I get an error dialogue to the effect that EF can't find the connection string for the context. Yet when I use the same variable in a form, all is well. It seems the user control is using a different context within which to look for the connection string than the usual app.config.
Public Class InvoiceWorkOrderSearch
Private _dataHelper As WorkOrderData = New WorkOrderData()
End Class

During Design time?
You can avoid this be only instancing the object if the control is in runtime mode.
The build in property to check for desing time (Me.DesignMode) is poor since it only tells you if you are currently designing the control itself. It returs false if you drop the usercontrol on a form.
You can use this code to check for designtime: http://dotnet-snippets.de/dns/designmode-workaround-windows-forms-SID299.aspx
Public Class InvoiceWorkOrderSearch
Private _dataHelper As WorkOrderData
Public Sub New()
If IsDesignMode(me) = False Then
_dataHelper = New WorkOrderData()
End If
End Sub()
End Class

Related

Was unable to turn lazy loading off in VB in EF6

VS2013, code first EF6, VB
Elsewhere on SO I found a post that led me to add this to my Context class:
Public Sub New()
Me.Configuration.LazyLoadingEnabled = False
End Sub
However, in order to load a secondary table into my context for a view to find data to list in a For Each loop I had to add:
Dim myQuery = db.Questions.Include("PossibleAnswers").Where(Function(x) x.QuestionID = 6).Single()
Without that query in one form or another, my view does not find any data in the property 'PossibleAnswers' and nothing is displayed during the For Each loop. But with the query above, the For Each finds the PossibleAnswers data.
I checked the value of
db.Configuration.LazyLoadingEnabled
just before my view was called and it was false. But I still had to make a query with the Include() method to force the data to be brought into the context.
This is the full definition of the table in question:
Public Class Question
Public Enum qType
TrueFalse
MultipleChoice
ShortAnswer
End Enum
Public Property QuestionID As Integer
Public Property Text As String
Public Property Type As qType
Public Property PossibleAnswers As New List(Of qAnswer)
Public Property UsedBySurveys As New List(Of qSurvey)
End Class
Can anybody suggest what I may not be understanding about this?
Thanks.
Best Regards,
Alan
If you want all results from your context to automatically load the navigation properties, you should remove the Me.Configuration.LazyLoadingEnabled = False line from your context class.
You can also explicitly set lazy loading to false for an instance of your context: ie.
Using db As New MyContext
db.Configuration.LazyLoadingEnabled = True
Dim myQuery = db.Questions.Where(Function(x) x.QuestionID = 6).Single()
End Using
With Lazy Loading set to false, you have to explicitly tell the EF to include navigation properties along with the result set. You do that by using the .Include function (as you did). Calling the .Include function is called Eager Loading.
If you set the lazy loading property to true, the navigation properties will be pulled back from the database automatically. Here's a quick run down of Lazy Loading from MSDN:
http://msdn.microsoft.com/en-us/library/vstudio/dd456846(v=vs.100).aspx

Why is design-time serialization failure occurring for a child control of a panel located in a custom user control?

After breaking down all possible causes of the issue, I have arrived with the following situation. I have a custom user control containing a panel and a control (button, label etc...) within this panel. I have also created public read only properties for the panel as well as the child control of the panel. Each of these properties has their DesignerSerializationVisibility attribute set to Content. When configured this way no code is serialized for the child control of the panel when the custom user control is used on a form. Upon building the control, the child control either disappears or experiences an incomplete rendering at design-time. If I change either the panel or its child control’s DesignerSerializationVisibility to visible, everything serializes and renders correctly. However, this then prevents me from being able to provide the end user with the ability to adjust either the panel or child controls property values. This one truly has me stumped!
UPDATE:
After getting home and performing the same steps on my development VM (also a fresh installation of VS2013) there are no issues. I am very confused at why this issue would be occurring on my work computer and not another. Any ideas are greatly appreciated as the mystery deepens...
Public Class ExampleUserControl
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property LeftPanel As Panel
Get
Return Me.ExampleLeftPanel
End Get
End Property
<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Control"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property LeftPanelButton As Button
Get
Return Me.ExampleLeftButton
End Get
End Property
<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property RightPanel As Panel
Get
Return Me.ExampleRightPanel
End Get
End Property
<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Control"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public ReadOnly Property RightPanelButton As Button
Get
Return Me.ExampleRightButton
End Get
End Property
End Class
I'm still not sure why this issue was occurring. However, after moving the custom user control into my control library (a separate .dll), all is working as expected. I suspect it was a glitch in the timing when compiling. Possibly it was trying to build the controls and serialize the code at the same time.

Memory Leak in VB.Net Application, Object held open by Grid?

I have a large, complex Windows Forms app written in VB.Net. Users are experiencing some memory issues and using JetBrains dotTrace Profiler to try and clear a few of these up.
There is still something holding some of my objects open. I have a 'Customer' object, which has a Generic.List of InvoiceLineItem. This item is basically an object bound to a grid control (ConponentOne FlexGrid) which has a load of readonly properties for displaying data, for example:
Public Class InvoiceLineItem
Private _customer as Customer
Private _charge as Charge
Sub New(Customer as Customer, Charge as Charge)
_customer = Customer
_charge = Charge
End Sub
Public ReadOnly Property Name as String
Return _customer.Name
End Property
Public ReadOnly Property ItemName as String
Return _charge.Name
End Property
Public ReadOnly Property Amount as Decimal
Return _charge.Amount
End Property
End Class
etc.
This object looks like it is not getting released from the FlexGrid.
The Flexgrid is on child form, shown from the main form. When the child form is closed, the Memory Profiler is showing that the form is still referenced.When I click "shortest Path" in dotTrace, the path below is shown.
This appears to be the only object in Customer with a root path.
There is no custom event handling going on in this form between my object or collection, and nothing is disposed.
What should I do to troubleshoot this further?
I found the issue. The FormClosing event was being handled elsewhere and tested for unsaved data, the reference to the form was being held in that function and not released!

Turn private variable to public in runtime?

please see image attached.
i want it to be
Public components As System.ComponentModel.lContainer
but every time I edit the form design, it always change back to its original code
Private components As System.ComponentModel.lContainer
You can have a public Property in your class that returns a private member, like yours. This code is generated by form designer and probably you can not change the behavior of this. Even if you could, I wouldn't suggest you to do this.
Public Class MyForm
Public Property Container As System.ComponentModel.IContainer
Get
Return Me.components
End Get
End Property
End Class
Cheers

problem with vb.net doing action for other forms

I have 2 forms, 1 is for control and the other is for display. Its 2 because the 1 for display will consume the whole screen and there's no space for buttons.
Im trying to control form4 using form2 or some other form, navigating a certain webpage in the process.
I have this code for the control:
but it does not work, what would I do?
Form4.WebBrowser1.Navigate("http://www.animedreaming.com/fairy-tail-episode-19/")
If I get your meaning you can access one form from another by making a local class along the lines of...
FORM#1 -->
Public Class FORM#1
Inherits System.Windows.Forms.Form
Public Shared Form as FORM#1
Public Sub someAction (byVal someParameter as someKindOfThingy)
' do something
End Sub
End Class
FORM #2 -->
FORM#1.someAction(someParameter)
===================================
There are any number of variations on this basic theme... from declaring a local class in FORM#2 as type FORM#1 through the use of delegates.