Hi Michael
I am doing the same and have conquered the first hurdle, I shall explain in a moment. However (as usual) whilst it works and applies updates, it causes an ObjectDisposedException sometime after the updates have been applied. This is caused by a <table> getter access to the DataContext after it has been disposed but there is no stack trace to go on. I have a feeling it is caused by Reflection somewhere in the DNN framework. Anyway, back to how to do it - or at least one way to do it...
Firstly I have created a base control used by each of my DNN controls. On this I have a property called Dcx of type HelpDeskDataContext (my dbml)
Private _dcx As HelpDeskDataContext
Protected ReadOnly Property Dcx() As HelpDeskDataContext
Get
If _dcx Is Nothing Then
_dcx = GetDataContext()
End If
Return _dcx
End Get
End Property
and a Private function as follows:-
Private Function GetDataContext() As HelpDeskDataContext
Dim dcx As HelpDeskDataContext = New HelpDeskDataContext(System.Configuration.ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
Return dcx
End Function
So at runtime, the DataContext is constructed using a secondary constructor where I can force the issue regarding the connectionstring.
On a typical control, you can place a LinqDataSourceControl (DsComments) but do not set its ContextTypeName whilst setting properties such as EnableUpdate etc. Instead, subscribe to the ContextCreating event as follows:-
Protected Sub DsComments_ContextCreating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceContextEventArgs) Handles DsComments.ContextCreating
e.ObjectInstance = Dcx
With DsComments
.TableName = "hdd_Comments"
.ContextTypeName = "Aureole.Modules.HelpDesk.HelpDeskDataContext"
.Where = "Issue_Id == @Issue_Id"
.WhereParameters.Add(New Parameter("Issue_Id", DbType.String))
.OrderBy = "CreatedDate desc"
End With
End Sub
You can then easily tie up your visual controls and data binding. If using a formView, I do this on the formView.ItemCreated event since by that time, the DataSource will be fully operational and the visual controls will be accessible in the relevant formView template.
By putting the DataContext into the ancestor control it is easily available to each control in my application. I also have properties in the base control to provide lists for lookups (comboboxes) so they are easily available whenever needed. For example:-
Private _products As List(Of hdd_Product)
Protected ReadOnly Property Products() As List(Of hdd_Product)
Get
If _products Is Nothing Then
_products = (From prods In Dcx.hdd_Products).ToList
End If
Return _products
End Get
End Property
Anyway, I am no expert and am groping my way through with both DNN and ASP.Net but discovered your post whilst seeking an answer to my own problem.
So, if it is of use then great, if anyone has ideas regarding the ObjectDisposedException then I would be keen to hear about them. In the meanwhile if I discover the root (and solution) to the problem I shall post it back here.
John