Save an aspx page in session - asp.net

I have a login page that return the userName to a page called User.aspx. The User.aspx shows the information about the user based on a sql select. I fill 12 labels on the page with the result of the select. I want to keep the information of my 12 labels if the user Quits and enter again in the page.
I save my session
Session("UserPage") = (litHello.Text And litAddresse.Text And litAnimalGenre.Text And litCouriel.Text And litNomAnimal.Text And litPays.Text And litPostalCode.Text And litProvince.Text And litRace.Text And litTel.Text And litVille.Text)
Now how I can proceed too fill all my label with my saved session call UserPage??? That is the question !!! The code is VB.NET
Thank's for helping me

Answer on your question:
I would make a class with multiple properties and save that in the session ;)
Eg.
Public class PropertySession
Public Property ID as integer
Public Property Name as String
Public Property Address as String
End Class
Then (the long way)
Dim currentPropertySession as PropertySession
With PropertySession
.ID = litID.Text
.Name = litName.Text
.Address = litAddress.Text
End With
And finally store
Session("Property") = currentPropertySession
or the shorter way (still need to declare PropertySession)
Session("Property") = New PropertySession With { .ID = litID.Text, .Name = litName.Text, .Address = litAddress.Text}
You can even do this - only to be complete, but i wouldn't do this if i were you -
Session("Property") = New Object With { .ID = litID.Text, .Name = litName.Text, .Address = litAddress.Text}
Additional
There are 8 ways to store data of a user to the next page.
Check out which one is good enough for you.

You can continue to do it the way you have it and then when you read out the session split the values into an array and then loop through your array. You will need to use a delimiter to separate the values so you can split them.
Session("UserPage") = (litHello.Text & "|" & litAddresse.Text & "|" & litAnimalGenre.Text & "|" & litCouriel.Text & "|" & litNomAnimal.Text & "|" & litPays.Text & "|" & litPostalCode.Text & "|" & litProvince.Text & "|" & litRace.Text & "|" & litTel.Text & "|" & litVille.Text)
The when you read out the the values:
dim userInfo as string() = Session("UserPage").toString().split("|")
Now set your labels accordingly
label1.text = userInfo(0)
label2.text = userInfo(1)
etc...
The one problem here is you have to be sure all your values in the session have a value and if they don't you fill it with an empty string "" so that your split will populate the correct number of values.

For authentication things you can use out of the box functionality from ASP.Net. It offers an API with standard providers like SQL Membership Provider etc.
By using web.config settings, you can allow users to access only certain portions of your site (like a members area). Then, if you like to query more information than the Membership Provider API allows you to, you can get the authenticated user identity (you natural key) from anywhere like:
Page.User.Identity.Name
With this information, you can query your database.

Related

Looping through different dropdown lists

I have multiple controls on a page that are all similar and are all numbered. For instance, I have multiple month controls like this:
Replacement1MonthDropDownList
Replacement2MonthDropDownList
Replacement3MonthDropDownList
Etc.
But when I have common code that works on all of the controls, I need a big Select Case statement like this:
Select Case Count
Case 1
Call Me.FillReplacements(rf.Replacements(0), Me.Replacement1MonthDropDownList, Me.Replacement1AmountTextBox, Me.ReplacementSaveButton)
Case 2
Call Me.FillReplacements(rf.Replacements(0), Me.Replacement1MonthDropDownList, Me.Replacement1AmountTextBox, Me.ReplacementSaveButton)
Call Me.FillReplacements(rf.Replacements(1), Me.Replacement2MonthDropDownList, Me.Replacement2AmountTextBox, Me.SplitButton1)
Is it possible to loop through the controls and get them by name--justreplacing the numbers in the name with the current Count in my loop?
Sorry, I'm very new to Visual Basic! :S
Yes, you can. The Page class (Me, in this case) has a FindControl method which allows you to find a control by name. So, for instance, you could do something like this:
Dim monthControl As Control = Me.FindControl("Replacement" & Count.ToString() & "MonthDropDownList")
Dim splitControl As Control = Me.FindControl("SplitButton" & Count.ToString())
If you need to cast them as a more specific type, you could use DirectCast. For instance:
Dim monthControl As DropDownList = DirectCast(Me.FindControl("Replacement" & Count.ToString() & "MonthDropDownList"), DropDownList)
Alternatively, and perhaps preferably, you could make an array of controls so you could access them by index. For instance, if you had an array like this defined:
Private monthControls() As DropDownList = {Replacement1MonthDropDownList, Replacement2MonthDropDownList, Replacement3MonthDropDownList}
Then you could access it by index like this:
Dim currentMonthControl As DropDownList = monthControls(Count)

Access 2007 - Referring to sub-report value from sub-sub-report

I have a report that has several sub-reports as well as a sub-report to one of the sub-reports.
The one that concerns me:
rptProgressReport ->rptEmployee (sub of rptProgressReport) -> rptSubEmployeeProject (sub of rptEmployee)
So far everything is generating as needed. Employees list in order, and the sub-report pulls out various project details. Except I need to add together time spent from one table.
tblProject (main table) -> tblProjectHistory (related table to tblProject via projectID->fldProjectID).
tblProjectHistory has the following fields.
[historyID],[fldProjectID], [History Information], [History Date], [Time Spent], [Employee].
I need to do a sum of all [Time Spent] for projects that equal what is being displayed and as long as the employee matches and the date is within the specified date range.
Specified date range is via the launching Form (frmReportGeneration) with fields txtStartDate and txtEnd Date.
Sub-report rptSubEmployeeProject has a text box (txtTimeSpent) that I have the following for a control source.
=Sum(DLookUp("[Time Spent]","tblProjectHistory","[Employee]='" & [Reports]![rptEmployee].[txtTempEmployee] & "' AND [History Date] > " & [Forms]![frmReportGeneration].[txtStartDate] & " AND " & [History Date]<" & [Forms]![frmReportGeneration].[txtEndDate] & "))
the rptEmployee field of txtTempEmployee correctly displays the current employee to match in that sub-report.
The problem is I get prompted for each value part of the above code - txtTempEmployee and txtStartDate/txtEndDate, even if I change the report value to be [Reports]![rptProgressReport]![rptEmployee].[txtTempEmployee]
Any idea how to correctly pull variables from the parent report or the first sub-report?
Thank you.
+++++ Update +++++
Okay update/close on this. I ended up needing to do something similar to what was suggested in the accepted answer. I could not get the idea posted to work - but i was able to set tempvars in vba and used those throughout the report/sub-report(s).
it is not recommended to refer to other objects via this construct "Forms!...." (or "Reports!...")
i made some bad experience with it. i suggest to put the values into a variable and make a
Get-Funktion:
in a Module you define:
Publicg dtStart As Date
Public gdtEnd As Date
in the form you assign the start and end date in the button where you fire the report
gdtStart = Me!txtStartDate
gdtEnd = Me!txtEndDate
now the Get-Function:
Public Function Get_Date_Start () As Date
Get_Date_Start = gdtStart
End Function
Public Function Get_Date_End () As Date
Get_Date_End = gdtEnd
End Function
in the Query you can use it now like this:
... AND [History Date] > " & Get_Date_Start() & " AND " & [History Date] <" & Get_Date_End()
BTW: don't use spaces in any object-name: instead of [History Date] name it History_Date, you can avoid Brackets in Code :-)

Cast a string to a name of a web label

HI, using vs2008 and building a web app. On a asp page called blackjack.aspx, I have four labels with id of lbBJTStatusP1 lbBJTStatusP2 lbBJTStatusP3 lbBJTStatusP4.
I want to address those labels in a single sub by casting the casting two strings into the control name, so that string lbBJTStatusP & "1" would refer to lbBJTStatusP1.This is done on the code behind page.
So far I have tried this but with no success. boxct refers to either "1" "2" "3" or "4".
DirectCast(blackjack.Controls.Find("lbBJTStatusP" & boxct, True)(0), Label).BackColor = stoodcolor
Can it be done and if so how. Thanks for all and any help.
You can't "cast" a string to a specific instance of a control.
What you can do is use FindControl: that accepts a string, searches (one level deep, not more) for a control with that name and returns it. The method returns a Control, so you might need to cast it to Label.
I have labels named lblqu01ex - lblqu10ex. I set the text value through coding as follows.
for i = 1 to 10
ex = "lbl" & IIf(i = 10, "qu10", "qu0" & i) & "ex"
DirectCast(FindControl(ex), Label).Text = 2*100/i
next
Its work.

Accessing VB6 Collection Item from VBScript embedded in HTML

i'm learning by practice. I was given an OCX file which according to who gave it to me was created using VB6 and I have the task of creating a user interface for it to test all the functionality that is described in a poorly written documentation file. On top of that I am not well-versed in VBScript but I've managed to dodge a few bullets while learning.
I have a method which returns a Collection and when I try to access it from VBScript I am only able to query the Count but when I try to do job.Item(i) or job(i) I get an error stating it doesn't have that property or method.
Can someone point me in the right direction to be able to traverse the contents of this collection?
I had to do it from JavaScript but since some things weren't that easy I decided that perhaps VBScript would help me bridge the gaps where JavaScript didn't cut it. I can access all properties from the ActiveXObject from JavaScript, but the methods which return other VB objects are a little more obscure to me. I've tried aJob.Item(iCount), aJob.Items(iCount) and aJob(iCount).
My code is:
For iCount = 1 To aJobs.Count
MsgBox("Num " & iCount)
MsgBox(aJobs.Item(iCount))
Next
Thanks.
People often create specialized and/or strongly typed collection classes in VB6. They don't always do it correctly though, and they sometimes create "partial" collection implementations that have no Item() method (or fail to mark it as the default member of the class). They might even have a similar method or property but name it something entirely different.
It is rarer to return a raw Collection object, but it can be done and if it is you shouldn't have the problems you have indicated from VBScript.
I just created a DLL project named "HallLib" with three classes: Hallway, DoorKnobs, and DoorKnob. The DoorKnobs class is a collection of DoorKnob objects. The Hallway class has a DoorKnobs object that it initializes with a random set of DoorKnob objects with randomly set properties. Hallway.DoorKnobs() returns the DoorKnobs collection object as its result.
It works fine in this script:
Option Explicit
Dim Hallway, DoorKnobs, DoorKnob
Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()
MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)
For Each DoorKnob In DoorKnobs
MsgBox "DoorKnob.Material = " & CStr(DoorKnob.Material) & vbNewLine _
& "DoorKnob.Color = " & CStr(DoorKnob.Color)
Next
Update:
This script produces identical results:
Option Explicit
Dim Hallway, DoorKnobs, KnobIndex
Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()
MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)
For KnobIndex = 1 To DoorKnobs.Count
With DoorKnobs.Item(KnobIndex)
MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
& "DoorKnob.Color = " & CStr(.Color)
End With
Next
As does:
Option Explicit
Dim Hallway, DoorKnobs, KnobIndex
Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()
MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)
For KnobIndex = 1 To DoorKnobs.Count
With DoorKnobs(KnobIndex)
MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
& "DoorKnob.Color = " & CStr(.Color)
End With
Next
So I suspect you'll need to use some type library browser like OLEView to look at your OCX to see what classes and members it actually exposes.

How do I traverse a collection in classic ASP?

I want to be able to do:
For Each thing In things
End For
CLASSIC ASP - NOT .NET!
Something like this?
dim cars(2),x
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars
response.write(x & "<br />")
Next
See www.w3schools.com.
If you want to associate keys and values use a dictionary object instead:
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
if objDictionary.Exists("Name") then
' Do something
else
' Do something else
end if
Whatever your [things] are need to be written outside of VBScript.
In VB6, you can write a Custom Collection class, then you'll need to compile to an ActiveX DLL and register it on your webserver to access it.
The closest you are going to get is using a Dictionary (as mentioned by Pacifika)
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = vbTextCompare 'makes the keys case insensitive'
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
But I loop through my dictionaries like a collection
For Each Entry In objDictionary
Response.write objDictionary(Entry) & "<br />"
Next
You can loop through the entire dictionary this way writing out the values which would look like this:
Scott
20
You can also do this
For Each Entry In objDictionary
Response.write Entry & ": " & objDictionary(Entry) & "<br />"
Next
Which would produce
Name: Scott
Age: 20
One approach I've used before is to use a property of the collection that returns an array, which can be iterated over.
Class MyCollection
Public Property Get Items
Items = ReturnItemsAsAnArray()
End Property
...
End Class
Iterate like:
Set things = New MyCollection
For Each thing in things.Items
...
Next
As Brett said, its better to use a vb component to create collections. Dictionary objects are not very commonly used in ASP unless for specific need based applications.
Be VERY carefully on using VB Script Dictionary Object!
Just discover this "autovivication" thing, native on this object: http://en.wikipedia.org/wiki/Autovivification
So, when you need to compare values, NEVER use a boolen comparison like:
If objDic.Item("varName") <> "" Then...
This will automatically add the key "varName" to the dictionary (if it doesn't exist, with an empty value) , in order to carry on evaluating the boolean expression.
If needed, use instead If objDic.Exists("varName").
Just spend a few days knocking walls, with this Microsoft "feature"...
vbscript-dictionary-object-creating-a-key-which-never-existed-but-present-in-another-object

Resources