I have a simple webcontrol.ascx with an listview and 2 Integer fields (show nbr. of certain elements). The Listview gets filled from an asp:ObjectDataSource.
My Problem is that both the listview and my 2 integer values need access to the same database table and I really dont want to make that call twice (Inside the Page_Load for the 2 fields and inside the SelectMethod for the ObjectDataSource)
The Problem is that I dont see a simple way for the two methods (Page_Load and SelectMethod) to exchange any data (with local properties for example).
private mydata As List(of ...)
protected Sub Page_Load(...) Handles Me.Load
mydata = DbManager.HeavyCall(...)
literalValueA.Text = (From i in mydata ..... ).Count
literalValueB.Text = (From i in mydata ..Where ... ).Count
End Sub
' SelectMethod for asp:ObjectDataSource
public Function GetData( ... ) As List(of ...)
mydata.DoSomething(...) ' mydata is Nothing here...
end Function
I hope someone can tell me a good solution (preferable without external cache..) of how to exchange data between the two methods..
The sad solution is: dont use the asp:ObjectDataSource...
Now Im using the asp:LinqDataSource wich provides a 'selecting'-event from where I can modify the Controls on my frontend.
' Selecting-Event from my LinqDataSource
private Sub myLinqDataSource_Selecting(sender .., e .. ) Handles myLinq..Selecting
dim mydata = DbManager.HeavyCall(..)
literalValueA.Text = (From i in mydata ..... ).Count ' works
literalValueB.Text = (From i in mydata ..Where ... ).Count ' works
e.Result = mydata
End Sub
Related
I'm using ASP.NET and Visual Basic to make a custom form, used for filling in information. Once I click a button, a function gets called that takes the bits information from that form, puts them into an object, and adds that object to a list. This is being used for a sort of queued entry system, so the form will be edited and submitted multiple times.
For some reason, instead of adding the new object in the next index position of the list, it instead replaces whatever was at 0. So, there is only ever one object in the list at the time.
Here's the custom form:
Here's my custom object, which is currently placed above my _Default class:
Public Class QueueItem
Public Property _TestName As String
Public Property _ValueID As String
Public Property _MathOperator As String
Public Property _ValueInput As Integer
Public Sub New()
End Sub
Public Sub New(ByVal TestName As String, ByVal ValueID As String, ByVal MathOperator As String, ByVal ValueInput As Integer)
_TestName = TestName
_ValueID = ValueID
_MathOperator = MathOperator
_ValueInput = ValueInput
End Sub
End Class
The list is declared above my Page_Load function, inside the _Default class, and is public. Here's that list definition:
Public QueueList As List(Of QueueItem) = New List(Of QueueItem)()
And, here's what gets called when that "Add To Queue" button is pressed:
Protected Sub AddToQueueButton_Click(sender As Object, e As EventArgs) Handles AddToQueueButton.Click
'Adds a new QueueItem to the QueueList
'Values pulled from the dropdown lists in the custom form
QueueList.Add(New QueueItem() With {
._TestName = TestName.SelectedValue,
._ValueID = ValueID.SelectedValue,
._MathOperator = MathOperator.SelectedValue,
._ValueInput = ValueInput.Text
})
'Below section is for testing
Dim test1 As String = QueueList(0)._TestName
Dim test2 As String = QueueList(0)._ValueID
Dim test3 As String = QueueList(0)._MathOperator
Dim test4 As String = QueueList(0)._ValueInput
Dim testmessage As String = test1 + " | " + test2 + " | " + test3 + " | " + test4
Dim count = QueueList.Count
Dim capacity = QueueList.Capacity
Response.Write("<script language='javascript'>alert('" + testmessage + "');</script>")
End Sub
So, as you can see, I have some test variables and stuff that I'm using to make sure this is working. Any time this gets called, an object gets added to the list, I look at the count and capacity for the list, and I display all the information in an alert.
This information for the alert is always reading from index 0. So, it shouldn't matter how many times I add information to the list, 0 should stay the same, and objects should be added at 1, then 2, and so on. Right?
Well, 0 changes any time I submit new information, and neither the count or capacity never increase past the first entry. They always display as if there's only one item in the list.
Here's me running the queue entry form twice, with two different numbers on the end:
First run:
Second run:
Since I'm always reading from index 0, that number at the end shouldn't change. It should be giving me the number that's associated with the object at index 0. And, the List.Add function should make the count and capacity go up. But, none of that happens. Instead, it seems to be replacing what was at 0.
If anyone has any tips on how to fix this or can clue me in on what might be going on, that would be greatly appreciated.
Every time a request is made to your page, a new instance of your class is created to handle the request. For a post-back request, some data may be loaded - for example, the values of the fields posted to the server, and anything stored in ViewState. But data stored in fields within your page will not be persisted across requests.
The code is not replacing the item at index 0; it is adding an item to an empty list.
You need to persist your list between requests somehow. You can either store it in the Session, or put it in the ViewState.
I have a form called "FrmClientesEdicion" which has a postal code field, I am trying to open the postal code file and return to the first form the information selected in the second form from the datagrid. What happens is that with the code so far the info does not pass to me but rather it opens a new instance of the form "FrmClientesEdicion", help?
Open the postal code form "FrmCodigosPosAr" from a button in "FrmClientesEdicion" with this line:
Dim f As New FrmCodigosPosAr
f.ShowDialog()
Search in the form : "FrmCodigosPosAr" select a row and pass data (p.e Code, Name) again to the first one: "FrmClientesEdicion" with this lines:
Try
Dim F As New FrmClienteEdicion
With F
.TxtCPOCLI.Text = DG.CurrentRow.Cells(0).Value.ToString ' The code
.TxtPOBCLI.Text = DG.CurrentRow.Cells(1).Value.ToString ' The name city
.TxtPROCLI.Text = DG.CurrentRow.Cells(2).Value.ToString ' The state
End With
Catch ex As Exception
MsgBox("Verifique: " & ex.Message.ToString, MsgBoxStyle.Critical)
End Try
Me.Close()
This second point doesn't works open a new instance but don`t...
Thanks again
Note: The first form still open, never close it when I open the second one!
The pictures:
Open form 2
Then pass data from FORM2 to the FORM1 AGAIN
I answer myself in case someone needs it
In form2 (FrmCodigosPosAr)
Public Class FrmCodigosPosAr
Inherits System.Windows.Forms.Form
Public myCaller As FrmClienteEdicion
In form 1, (p.e in a Button to call form2 (FrmCodigosPosAr))
Dim myform As FrmCodigosPosAr
Private Sub BtnBuscaCP_Click(sender As Object, e As EventArgs) Handles BtnBuscaCP.Click
If myform Is Nothing Then
myform = New FrmCodigosPosAr
myform.myCaller = Me
End If
myform.Show()
End Sub
And in the form 2 (FrmCodigosPosAr) to pass data to Form1 (FrmClientesEdicion)
Try
If Not myCaller Is Nothing Then
myCaller.Text = Now.ToLongTimeString
myCaller.TxtCPOCLI.Text = DG.CurrentRow.Cells(0).Value.ToString
myCaller.TxtPOBCLI.Text = DG.CurrentRow.Cells(1).Value.ToString
myCaller.TxtPROCLI.Text = DG.CurrentRow.Cells(2).Value.ToString
End If
Catch ex As Exception
MsgBox("Verifique: " & ex.Message.ToString, MsgBoxStyle.Critical)
End Try
Me.Close()
Hello i got a drop down list with post back from worker data base .
I also got a grid view with that data base.
when i choose a worker on the drop down i got 5 text boxes that are being filled from the right row on the grid view.
My question is how can i do that without using the auto post back?
p.s I am using visual studio with vb.net
Thanks in advance!
atm i am using something like this but i am really trying to remove all not needed postbacks from my site.
Protected Sub workerDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles workerDropDownList.SelectedIndexChanged
Dim x As Integer
For x = 0 To workerDataGrid.Rows.Count - 1
If workerDataGrid.Rows.Item(x).Cells(1).Text = workerDropDownList.SelectedValue Then
editWorkerNameBox.Text = workerDataGrid.Rows.Item(x).Cells(0).Text
editWorkerIdBox.Text = workerDataGrid.Rows.Item(x).Cells(1).Text
editPhoneBox.Text = workerDataGrid.Rows.Item(x).Cells(2).Text
editCellPhoneBox.Text = workerDataGrid.Rows.Item(x).Cells(3).Text
editEmailBox.Text = workerDataGrid.Rows.Item(x).Cells(4).Text
editUserNameBox.Text = workerDataGrid.Rows.Item(x).Cells(5).Text
editUserPassBox.Text = workerDataGrid.Rows.Item(x).Cells(6).Text
editIsManagerBox.Text = workerDataGrid.Rows.Item(x).Cells(8).Text
editIsActiveBox.Text = workerDataGrid.Rows.Item(x).Cells(7).Text
End If
Next
End Sub
I'm currently working on coding a web page for a school project. This site is supposed to be a simple online store where people can order prints of artwork. The specific page I'm working on has a Drop Down List (ddlArt) that is bound to my database and displays a list of the different art pieces available. When the user selects one of the items, all the information on that item is pulled from the database and displayed on the page in a variety of labels and such. The only thing is that I'm getting a null reference exception error saying "Object reference not set to an instance of an object" when I go to try to run the page. I got the same error on a homework assignment earlier in the year and managed to get it fixed, but I can't remember what I did and I can't get help from school until next week, so I thought I'd try my luck on here. Here's my code:
Private selectedArt As Art
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
End If
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End Sub
Private Function GetSelectedArt() As Art
Dim artTable As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'"
Dim artRow As DataRowView = artTable(0)
Me.imgArt.ImageUrl = "~/images/" & artRow("FileName")
Dim art As New Art
art.ArtID = artRow("ArtID").ToString
art.ArtName = artRow("ArtName").ToString
art.Caption = artRow("Caption").ToString
art.Description = artRow("LongDescription").ToString
art.FileName = artRow("FileName").ToString
Return art
End Function
And here's the code for the Art class, in case anybody is interested:
Public Class Art
Public Property ArtID As Integer
Public Property ArtName As String
Public Property ArtType As String
Public Property Caption As String
Public Property FileName As String
Public Property Description As String
End Class
When I get the error, it highlights the artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'" line in the GetSelectedArt function. I've tried comparing it to my corrected homework assignment that I mentioned, but I can't seem to find the problem. My VB is a little fuzzy because it's been awhile since I actually took the class. Any suggestions? Thanks a bunch!
If I understand your comment above correctly, on the initial page load there is nothing in the ddlArt, because the user must first choose an art type.
If that is correct, then your answer to my question is your answer.
For whatever reason (and without seeing at least the Select statement), artTbl is not getting instantiated, which is why you're seeing the Object reference not set to an instance of an object error.
One way to fix this (without knowledge of your SqlDataSource it's hard to give a precise answer) is to modify your Page Load method so that GetSelectedArt is only called when the user has selected an item from the drop down list. Right now GetSelectedArt is called every time the page loads.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
Else
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End If
End Sub
However, the above modification will only prevent GetSelectedArt from being called on the initial page load. If your SqlDataSource.Select command is still returning nothing, then you're still going to have this problem.
A better solution would be to call the GetSelectedArt on the ddlArt.SelectedIndexChanged event handler. This way you'll know that you have (or should have) a valid SelectedValue from ddlArt.
Also, if you don't populate the drop down until the user selects an art type from the radio button list, why are you binding the drop down list on the initial page load (and what are you binding it to)? Or is the drop down list and detail information on a different page from the radio button list?
May be .. with ArtID as integer
artTable.RowFilter = "ArtID = " & format(ddlArt.SelectedValue)
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