How to force linq to Sql to execute query straight away - asp.net

I have a repeater of controls on an aspx page. I am trying to move a query out of the control and into the parent page as there is a massive hit on the DB for each control. The problem is that I get the following error.
Cannot access a disposed object.
Object name: 'DataContext accessed after Dispose.'.
I don't understand this as I thought that doing .ToList() forces the query to be executed
My code in the Parent page
Private _activityList As IEnumerable(Of Activity)
Public ReadOnly Property ActivityList() As IEnumerable(Of Activity)
Get
Return _activityList
End Get
End Property
Sub PopulatePage()
Dim activityList = From a In dbContext.Activities Where a.PA.PA_Key.ToUpper().Trim() = "DCC" _
Select a
_activityList = activityList.AsEnumerable()
End Sub
The code in my control is:
Public _activityList As List(Of Activity)
Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
_activityList = CType(Me.Page, ParentPage).ActivityList.ToList()
End Sub
Sub grdSelectedActivities_NeedDataSource(ByVal source As Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles grdSelectedActivities.NeedDataSource
Dim lnqActivities = _activityList
Dim objActivity As Activity = (From x In lnqActivities Where x.AC_Code = ActivityCode Select x).Single
Dim lnqRoundActivities = (From roundactivity In objActivity.RoundActivities Where roundactivity.RA_DS_Code = DepartmentalSettingsCode
Select roundactivity Order By roundactivity.RA_Name)
grdSelectedActivities.DataSource = lnqRoundActivities
End Sub
EDIT
I think that is is failing as it is trying to get RoundActivities in the grdSelectedActivities_NeedDataSource control method. Therefore I need to send an Activities object which has RoundActivityies child objects.
I have tried to create this object but get the following error:
Explicit construction of entity type 'Activity' in query is not allowed.
enter code here
This is the updated code:
Dim activityList = (From a In dbContext.Activities Where a.PA.PA_Key.ToUpper().Trim() = "DCC" Select New Activity With {.AC_Code = a.AC_Code, .RoundActivities = a.RoundActivities})
Solution:
I followed #kristoferA advice and did the following
Dim loadOptions = New DataLoadOptions()
loadOptions.LoadWith(Of Activity)(Function(a As Activity) a.RoundActivities)
loadOptions.AssociateWith(Of Activity)(Function(a As Activity) a.RoundActivities.Where(Function(z) If(z.RA_DS_Code = departmentCode, False)))
dataContext.LoadOptions = loadOptions

Sounds like you are lazy loading some association.
Turn off lazy loading (dc.DeferredLoadingEnabled = false), and pass a DataLoadOptions object to dc.LoadOptions to instruct the DC what you want to eager load.

ai.farfa is correct with the ToList(). That is the call that trigger the actual query.
I think you should do it on this line though :
grdSelectedActivities.DataSource = lnqRoundActivities.ToList()

I guest that the dbContext get disposed after PopulatePage() done and your Linq select return type is IEnumerable(Of Activity) which's just a prepared SQL statement.
try..
_activityList = activityList.ToList()//.AsEnumerable()
Edit
if your Model is stateless you can create new dbContext then enumerate and dispose it.
Using db As New dbContext()
Dim activityList = (From a In dbContext.Activities Where a.PA.PA_Key.ToUpper().Trim() = "DCC" _
Select a).ToList()
_activityList = activityList.ToList()
End Using

Related

Unable to cast object of type 'VB$StateMachine_4_GetAllFrontPageBanners' to type IEnumerable(Of FrontPageBanner)

I'm not really used to working with VB.NET, but i've come across an error where googling did not suffice. I've created this data access class, it has a method that makes use of Yield and vb.net's object initializer shortcut
Public Class FMACMarketingRepository
Private cvConnection As SqlConnection
Private cvCommand As SqlCommand
Private cvReader As SqlDataReader
Public Sub New(Role As InfotelLibrary.Data.DatabaseRole)
cvConnection = New SqlConnection(InfotelLibrary.Data.ConnectionString(Role, InfotelLibrary.Data.DatabaseName.Mongoose))
End Sub
Public Iterator Function GetAllFrontPageBanners() As IEnumerable(Of FrontPageBanner)
Using dbConnection As IDbConnection = cvConnection
Using cmd As IDbCommand = dbConnection.CreateCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "sel_AllFmacFrontPageBanners"
Using reader As IDataReader = cmd.ExecuteReader()
If reader Is Nothing Then
Yield Nothing
End If
While reader.Read()
Yield New FrontPageBanner() With
{
.Banner_Id = CType(reader("Banner_ID"), Integer),
.Geo_Id = CType(reader("Geo_ID"), Long),
.Title = CType(reader("Title"), String),
.Description = CType(reader("Description"), String),
.Link = CType(reader("Link"), String),
.Image = CType(reader("Image"), Byte()),
.SpecialOffer = CType(reader("Special_Offer"), Boolean)
}
End While
End Using
End Using
End Using
End Function
End Class
There are 0 errors in Intellisense, it builds but when i run the webpage it get the error
System.InvalidCastException: Unable to cast object of type 'VB$StateMachine_4_GetAllFrontPageBanners' to type 'System.Collections.Generic.List`1[InfotelData.Mongoose.Data.FrontPageBanner]'.
Line 7:
Line 8: Protected Sub Page_Load(ByVal sender As Object, e As EventArgs) Handles Me.Load
Line 9: Dim banners As List(Of FrontPageBanner) = cvRepo.GetAllFrontPageBanners()
Line 10: If banners.Count() > 0 Then
Line 11: rptUploadedBanners.DataSource = banners
Debugging just gives the same error when it hits page_load.
I've the distinct feeling that user error is to blame.
GetAllFrontPageBanners returns an IEnumerable(Of FrontPageBanner)(*) and you're trying to store it inside a List(Of FrontPageBanner). I'm surprised it doesn't give a compile time error (you're probably in Option Strict Off mode)
you need to make a List from the enumerable using .ToList for example :
Dim banners As List(Of FrontBanner) = cvRepo.GetAllFrontPageBanners.ToList
(*) Internally an Iterator block transform the function into a generated "class-state-machine" (which implement IEnumerable(Of FrontBanner) in your case).
That's the odd name you got but you can (and should) consider it like the return type given in your source code.
More information about that here

Get product inventory list from Magento API v2 using asp.net

Just figured out how to get a sessionid back using http://xxx.xxx.xxx/api/v2_soap?wsdl and nothing seems to work. The code below returns the following:
Line that has error:
prodListRequest = ws.catalogProductList(sessionId, filter, "")
Error returned:
Value of type '1-dimensional array of CompanyADevMagento.catalogProductEntity' cannot be converted to 'MagentoService.catalogProductReturnEntity'.
Does anyone have a asp.net vb example of getting inventory list?
Thanks.
Imports CompanyADevMagento
Imports System.Xml
Partial Class site_magento
Inherits System.Web.UI.Page
Dim sessionId As String = ""
Protected Sub btnShowData_Click(sender As Object, e As System.EventArgs) Handles btnShowData.Click
Dim ws As New CompanyAMagento.MagentoService
Dim sOutput As String = ""
sessionId = ws.login("username", "yourapipassword")
litOutput.Text = "Session: " & sessionId
Try
Dim filter As New filters
Dim prodListRequest As New MagentoService.catalogProductReturnEntity
prodListRequest = ws.catalogProductList(sessionId, filter, "")
Catch ex As Exception
litError.Text = ex.ToString
End Try
End Sub
End Class
-Magento Version using: 1.7.0.2
-Currently set Magento Core API > WS-I Compliance = Yes
I'm not too familiar with ASP, however it looks to me that your code is expecting the wrong object type.
The catalogProductList API method returns an instance of catalogProductEntity, while your code seems to expect catalogProductReturnEntity.
The latter is returned from the catalogProductInfo method.
CatalogProductEntity
CatalogProductReturnEntity
From the docs: http://devdocs.magento.com/guides/m1x/api/soap/catalog/catalogProduct/catalog_product.list.html

asp.net multithreading with synclock

i have some test code which i run at every load of a page in my asp.net website
this is the code
Sub TryThreads()
Dim t1 = New Thread(AddressOf TryLock)
t1.Priority = ThreadPriority.Lowest
t1.Start()
Dim t2 = New Thread(AddressOf TryLock)
t2.Priority = ThreadPriority.Lowest
t2.Start()
End Sub
Sub TryLock()
Dim LockObject = New Object
SyncLock LockObject
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
the "dotrace" simply add a record to a log table in the db
now the right result would be that i should have the entries in the db in order "entered","exiting","exited"
but actually when i look in the db i see first 2 "entered" then 2 "exiting" etc.
meaning that the multithreading is working ok, but not the synclock
is that correct?
and how can this be fixed?
the real code will be adding records to the db and might be called from several pages of different sessions, but the same code must not run twice concurrently
i do appreciate anybodys help
thank you very much!!!
EDIT:
in response to Sashas wonderful post i changed my code to a class (it was in a module) and now it looks like this:
Public Class CheckClass
Property LockObject As Object
Get
If HttpRuntime.Cache("CheckSessionsLock") Is Nothing Then HttpRuntime.Cache("CheckSessionsLock") = New Object
Return HttpRuntime.Cache("CheckSessionsLock")
End Get
Set(ByVal value As Object)
If value Is Nothing Then
HttpRuntime.Cache.Remove("CheckSessionsLock")
Else
HttpRuntime.Cache("CheckSessionsLock") = value
End If
End Set
End Property
Sub TryThreads()
Dim t1 = New Thread(AddressOf TryLock)
t1.Priority = ThreadPriority.Lowest
t1.Start()
Dim t2 = New Thread(AddressOf TryLock)
t2.Priority = ThreadPriority.Lowest
t2.Start()
End Sub
Sub TryLock()
SyncLock LockObject
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
End Class
now it works 80-90% of the time.
on page load i have:
Dim cc = New CheckClass
cc.TryThreads()
if i open multiple pages at once, they still clash some times. but if i'm correct, the issue is now not with the synclock as much as with the httpruntime.cache, because when using a standard property, on one page, the code works 100%.
so how can i make sure that 2 threads, even from totally different sessions never run the trylock simultaneously?
thank you all for helping out
You are creating a new object instance when the TryLock method is called, and use that for locking. If you want mutual exclusion between the two threads, you need to use a common object instance for locking, e.g. a static member of your class or a parameter that you pass to both threads.

How to add Transactions with a DataSet created using the Add Connection Wizard?

I have a DataSet that I have added to my project where I can Insert and Add records using the Add Query function in Visual Studio 2010, however I want to add transactions to this, I have found a few examples but cannot seem to find one that works with these.
I know I need to use the SQLClient.SQLTransaction Class somehow. I used the Add New Data Source Wizard and added the Tables/View/Functions I need, I just need an example using this process such as How to get the DataConnection my DataSet has used. Assuming all options have been set in the wizard and I am only using the pre-defined adapters and options asked for in this wizard, how to I add the Transaction logic to my Database.
For example I have a DataSet called ProductDataSet with the XSD created for this, I have then added my Stock table as a Datasource and Added an AddStock method with a wizard, this also if a new item calls an AddItem method, if either of these fails I want to rollback the AddItem and AddStock in this case.
In this example, I have a dataset called "dsMain" and a few direct queries in a "QueriesTableAdapter". I extend the partial class for the TableAdapter with a function that will create a transaction based on the first (0) connection and then apply it to every connection in the table adapter.
Namespace dsMainTableAdapters
Partial Public Class QueriesTableAdapter
Public Function CreateTransaction() As Data.IDbTransaction
Dim oConnection = Me.CommandCollection(0).Connection
oConnection.Open()
Dim oTrans = oConnection.BeginTransaction()
For Each cmd In Me.CommandCollection
cmd.Connection = oConnection
cmd.Transaction = oTrans
Next
Return oTrans
End Function
End Class
End Namespace
You begin the transaction by calling the new function
Dim qa As New dsMainTableAdapters.QueriesTableAdapter
Dim oTrans = qa.CreateTransaction()
Then you can call TableAdapter queries within your transaction
qa.Query1
qa.Query2
When you are done with your queries you commit the transaction
oTrans.Commit()
You can do the same thing for any TableAdapter that was created for your datasets.
If you have multiple TableAdapters that need to use the same transaction, then in addition to a "CreateTransaction" you should make a "SetTransaction" and have the Transaction be a parameter.
first of all thanks for your answer carter, it helped me very much!
but iam not able to handle the part with the parameters
You can do the same thing for any TableAdapter that was created for your datasets. If you have multiple TableAdapters that need to use the same transaction, then in addition to a "CreateTransaction" you should make a "SetTransaction" and have the Transaction be a parameter.
so iam able to handle 1 transactions with 1 tableadapter, but not 1 transaction with 2 tableadapters:
iam doing this for a school project, and i really need your help!!
here is the code to add a new material and a historical price to it(a changing price, like by fuel; iam saving it in an related table to material in the database):
Namespace DataSetTableAdapters
Partial Public Class MaterialPriceTableAdapter
Public Function SetTransaction() As Data.IDbTransaction
Dim oConnection = Me.CommandCollection(0).Connection
oConnection.Open()
Dim oTrans = oConnection.BeginTransaction()
For Each cmd In Me.CommandCollection
cmd.Connection = oConnection
cmd.Transaction = oTrans
Next
Return oTrans
End Function
End Class
Partial Public Class MaterialTableAdapter
Public Function CreateTransaction(ByVal MaterialPrice As System.Data.Odbc.OdbcTransaction) As Data.IDbTransaction
Dim oConnection = Me.CommandCollection(0).Connection
oConnection.Open()
Dim oTrans = oConnection.BeginTransaction()
For Each cmd In Me.CommandCollection
cmd.Connection = oConnection
cmd.Transaction = oTrans
Next
Return oTrans
End Function
End Namspace
`
and now the code in the form the form:
Public Class AddMaterial
Dim material As New DataSetBATableAdapters.MaterialTableAdapter
Dim materialprice As New DataSetBATableAdapters.MaterialPriceTableAdapter
Dim oTrans = material.CreateTransaction(materialprice.SetTransaction())
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
Try
material.InsertQuery(NameTextBox.Text, UnitComboBox.SelectedValue)
materialprice.InsertQuery(Date_BeginDateTimePicker.Value, PriceTextBox.Text, Date_EndDateTimePicker.Value, Me.LkwTableAdapter.ScalarQuery())
oTrans.Commit()
Catch ex As Exception
oTrans.Rollback()
MsgBox("Error by Insert")
End Try
Me.Close
End Sub
End Class
if i save a new record the materialprice.insertquery isnt commited by otrans.commit. what am i doing wrong? if you have an idea what it is, please tell me
thanks,
Xeras
This is untested, but this is how I imaging the CreateTransaction/SetTransaction combo should be written (with your OdbcTransaction object).
Public Function CreateTransaction() As System.Data.Odbc.OdbcTransaction
Dim oConnection = Me.CommandCollection(0).Connection
oConnection.Open()
Dim oTrans = oConnection.BeginTransaction()
SetTransaction(oTrans)
Return oTrans
End Function
Public Sub SetTransaction(ByVal oTrans As System.Data.Odbc.OdbcTransaction)
For Each cmd In Me.CommandCollection
cmd.Connection = oTrans.Connection
cmd.Transaction = oTrans
Next
End Sub

Must I use parameters with an ObjectDataSource update?

I have a business object that I've been using, which has a bunch of properties and a Save method, which inserts/updates to the database. The save method is NOT status, so the object needs to be instantiated, and the properties for the DB update/insert get pulled from the object.
Now I'm trying to bind the object to a FormView with the ObjectDataSource. I have it working so it instantiates based on the QueryString parameter, no problem, and populates the textboxes just fine. The UpdateMethod I have set to the Save function. Now it gets stuck.
It seems the ObjectDataSource needs a method with all the fields/properties/textboxes as parameters. I would have thought it would update the object's properties and then call the parameterless Save function. Is this wishful thinking?
Do I now need to change my Save function to include parameters, and change all the instances where it's getting used to this new method, just for this reason?
Thanks
Sean
Unfortunatly it does require params.
I overloaded my insert/update methods to include a few params. Attach the ObjectDataSource to the method with params.
The overloaded Update method calls the original Update method saving all the data. Seems kind of hackish to me, but it works.
Public Sub Update()
Dim isUpdated As Boolean = False
sql = "UPDATE AudioFiles SET Title = #Title, [desc] = #desc, Active = #Active WHERE fileID = #fileID"
conn = New SqlConnection(connString)
conn.Open()
...
End Sub
Public Sub Update(ByVal upFileID As Integer, ByVal upTitle As String, ByVal upDesc As String, ByVal upActive As Boolean)
Dim isUpdated As Boolean = False
Dim audioFile As New AudioFiles(fileID)
If Len(upTitle) > 0 Then
_title = title
End If
...
audioFile.Update()
End Sub

Resources