User Control ViewState/State Help needed - asp.net

I need help making this code work better. Currently, what happens is when it reloads, I loose the third value which is a calculated value . I would like for my page to reload without loosing the values for each property and each instance of the user control on the page.
Thanks in advance
Private _Length As Double = 0.0
Public Property Length() As Double
Get
If (Me.ViewState("calcLength") IsNot Nothing) Then
Return CType(ViewState("calcLength"), Double)
End If
Return _Length
End Get
Set(ByVal value As Double)
ViewState("calcLength") = value
txtLength.Text = value.ToString()
_Length = value
End Set
End Property
Private _Width As Double = 0.0
Public Property Width() As Double
Get
If (Me.ViewState("calcwidth") IsNot Nothing) Then
Return CType(Me.ViewState("calcwidth"), Double)
End If
Return _Width
End Get
Set(ByVal value As Double)
Me.ViewState("calcwidth") = value
Me.txtwidth.Text = value.ToString()
_Width = value
End Set
End Property
Private _calculatedboardfeet As Double = 0.0
Public Property CalculateBoardFeet() As Double
Get
If (Me.ViewState("calculateboardfeet") IsNot Nothing) Then
_calculatedboardfeet = CType(ViewState("calculateboardfeet"), Double)
End If
Return _calculatedboardfeet
End Get
Set(ByVal value As Double)
Me.ViewState("calculateboardfeet") = value
Me.lblCalculatedValue.Text = String.Format("{0:f2}", value)
_calculatedboardfeet = value
End Set
End Property

There's this portion which I 'think' doesn't make sense, though it may not be the cause of the problem. I've yet to studied the rest of the codes in detail.
ElseIf (Me.ViewState("txtwidth") Is Nothing) Then
Return CType(Me.ViewState("txtwidth"), Double)
If the ViewState item cannot be found (i.e. Is Nothing), how do you return the cast-ed value? This is the same for the 3 properties.

What do you mean by the "third value which is the calculated value"? Third as in CalculateBoardFeet()?
At what stage in the page life cycle are you calling these properties?
Why are you storing the values both in ViewState and in the control's .Text property, and then again in a class variable? Too. Many. Copies.
If you programmatically set the control's .Text property, that value should be restored on postback for you, without you having to explicitly set ViewState yourself.

Turns out what I needed was to call the assignment part of my code in the pre_Render event in the Usercontrol. It comes later than the Parent Page's PageLoad() event, which is when the information needed will be available in the control's viewstate

Related

Routine to compare new and old string values, maintain old data if nothing new

I have a Form which allows users to enter new Data for a record. In order to maintain Data Integrity, I copy all the pre-existing Data (if Any) into the Record and overwrite only those fields which the user has added new Data to. This new Record is saved to the database and the old Record Deleted.
I'm merely trying to find the most efficient way of going through each field to :
determine if there is Current Data to Maintain
New Data to Overwrite
I'll need to write the same type of Check for String, Decimal, Date and Integer (all nullable)
Here's my string check
Private Function CreateRecordforSubmission() As Business.Casualty
Dim c As Business.Casualty = Casualty
c.alias = CheckStringValues(c.alias, txtAlias.Text.Trim())
Return c
End Function
Private Function CheckStringValues(OldValue As String, newValue As String) As String
If Not OldValue Is Nothing AndAlso Not String.IsNullOrEmpty(newValue) Then
'<-- There is NEW DATA
If Not String.IsNullOrEmpty(newValue) Then
Return newValue
Else
'<-- No NEW DATA, check of there is CURRENT DATA to maintain
If Not OldValue Is Nothing Then
Return OldValue
Else
Return String.Empty
End If
End If
Else
'<-- NO CURRENT DATA AND no changes have been made
Return String.Empty
End If
End Function
Check out how CSLA does it. Each property in your object could take care of that comparison, if the setter detect a change or new value, you can update your variable and mark your object as modified.
Public Property Alias As String
Get
Return _alias
End Get
Set(ByVal value As String)
If Not String.IsNullOrEmpty(value) AndAlso _alias <> value Then
_alias = value
_objectWasModified = True
End If
End Set
End Property
The If statement could be in the base class, or a helper class.
If you want to have a generic function for other type, here's an example. This assume that you are using nullable values.
Public Property Number As Integer?
Get
Return _number
End Get
Set(ByVal value As Integer?)
If Not IsNullOrSame(Of Integer)(_number, value)Then
_number= value
_objectWasModified = True
End If
End Set
End Property
Public Function IsNullOrSame(Of T As Structure)(ByVal oldValue As Nullable(Of T), ByVal newValue As Nullable(Of T))
If newValue.HasValue Then
If Not oldValue.HasValue OrElse Not oldValue.Value.Equals(newValue.Value) Then
Return False
End If
End If
Return True
End Function

Understanding how Public Properties are set in VB.NET

I am trying to understand how a value in a Public property is set. There does not seem to be anything passed or any kind of lookup.
Public Shared Property IsUser() As String
Get
If HttpContext.Current.Session("IsUser") IsNot Nothing Then
Return HttpContext.Current.Session("IsUser").ToString()
Else
Return Nothing
End If
End Get
Set(value As String)
HttpContext.Current.Session("IsUser") = value
End Set
End Property
I have tried searching for the ClassName i.e. UserSession.IsUser = and also the only place the session value is set is in the property. HttpContext.Current.Session("IsUser") = value
What do I need to understand better.. I mean at the end of the day the User has to be True or False so where is the Lookup or passed parameter?

Can i use a single session variable to store all the field values

I have 9 pages with 10 fields in each page. Can i use a single session variable to store all the field(textbox,drop downlist,radiobuttons) values of 9 pages? If so could you give me small example inorder to proceed. Im kind of stuck.
Could you? Yes. Should you? Most likely not - though I can't say for sure without understanding what problem you are intending to solve.
Update with one sample solution
OK, I'm going to assume you want to store the values from the controls and not the controls themselves. If so, the easiest solution is stuff them in using some meaningful token to separate them. Like:
Session("MyControlValueList") = "name='txt1',value='hello'|name='txt2', value'world'"
To retrieve you would split them into a string array:
myArray = Session("MyControlValueList").Split("|")
And then iterate through to find the control/value you want.
So strictly speaking that's an answer. I still question whether it is the best answer for your particular scenario. Unfortunately I can't judge that until you provide more information.
Create a custom class with all the fields you want to save, then populate an instance of that and save that instance as a session variable.
I have something similar, but not identical - I'm saving various shipping address fields for an order, and I'm allowing the admins to update the order, either the shipping information or the order line items. Since that information is kept on separate tables, I store the shipping information in a session variable, and then compare it to what's on the form when they hit the "Update" button. If nothing has changed, I skip the update routine on the SQL Server database.
The easiest way for me to do this was to create a "OrderInfo" class. I saved the shipping information to this class, then saved that class to a session variable. Here's the code showing the class -
Public Class OrderInfo
Private v_shipname As String
Private v_add1 As String
Private v_add2 As String
Private v_city As String
Private v_state As String
Private v_zipcd As String
Private v_dateneeded As Date
Private v_billingmeth As Integer
Public Property ShipName() As String
Get
Return v_shipname
End Get
Set(value As String)
v_shipname = value
End Set
End Property
Public Property Add1() As String
Get
Return v_add1
End Get
Set(value As String)
v_add1 = value
End Set
End Property
Public Property Add2() As String
Get
Return v_add2
End Get
Set(value As String)
v_add2 = value
End Set
End Property
Public Property City() As String
Get
Return v_city
End Get
Set(value As String)
v_city = value
End Set
End Property
Public Property State() As String
Get
Return v_state
End Get
Set(value As String)
v_state = value
End Set
End Property
Public Property ZipCd() As String
Get
Return v_zipcd
End Get
Set(value As String)
v_zipcd = value
End Set
End Property
Public Property DateNeeded() As Date
Get
Return v_dateneeded
End Get
Set(value As Date)
v_dateneeded = value
End Set
End Property
Public Property BillingMeth() As Integer
Get
Return v_billingmeth
End Get
Set(value As Integer)
v_billingmeth = value
End Set
End Property
End Class
Here's the code for when I tested the concept to see if I could store a custom class in a session variable. This routine gets the order record, populates the fields in an instance of the custom class, and on the web form, as well. I save that instance to a session variable, then I initialize another new instance of that custom class, load the session variable to it. I then display the field values from the "retrieved" custom class, and what showed on the label matched what it should be -
Protected Sub LoadOrderInfo(ByVal ordID As Integer)
Dim connSQL As New SqlConnection
connSQL.ConnectionString = ConfigurationManager.ConnectionStrings("sqlConnectionString").ToString
Dim strProcName As String = "uspGetOrderInfoGeneral"
Dim cmd As New SqlCommand(strProcName, connSQL)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("OrderID", ordID)
If connSQL.State <> ConnectionState.Open Then
cmd.Connection.Open()
End If
Dim drOrderInfo As SqlDataReader
drOrderInfo = cmd.ExecuteReader
If drOrderInfo.Read Then
Dim orgOrder As New OrderInfo
orgOrder.ShipName = drOrderInfo("shipName")
orgOrder.Add1 = drOrderInfo("ShipAdd1")
orgOrder.Add2 = drOrderInfo("ShipAdd2")
orgOrder.City = drOrderInfo("ShipCity")
orgOrder.State = drOrderInfo("ShipState")
orgOrder.ZipCd = drOrderInfo("ShipZip")
orgOrder.DateNeeded = drOrderInfo("DateNeeded")
orgOrder.BillingMeth = drOrderInfo("BillingMethodID")
If Session.Item("orgOrder") Is Nothing Then
Session.Add("orgOrder", orgOrder)
Else
Session.Item("orgOrder") = orgOrder
End If
' I could just as easily populate the form from the class instance here
txtShipName.Text = drOrderInfo("shipName")
txtAdd1.Text = drOrderInfo("ShipAdd1")
txtAdd2.Text = drOrderInfo("ShipAdd2")
txtCity.Text = drOrderInfo("ShipCity")
txtState.Text = drOrderInfo("ShipState")
txtZipCd.Text = drOrderInfo("ShipZip")
selDate.Value = drOrderInfo("DateNeeded")
ddlBillMeth.SelectedValue = drOrderInfo("BillingMethodID")
End If
cmd.Connection.Close()
Dim retOrder As New OrderInfo
retOrder = Session.Item("orgOrder")
lblWelcomeMssg.Text = retOrder.ShipName & ", " & retOrder.Add1 & ", " & retOrder.City & ", " & retOrder.DateNeeded.ToShortDateString & ", " & retOrder.BillingMeth.ToString
End Sub
This might not be practical or desirable, given the number of fields you are trying to hold onto that way, but I'm not here to judge, so this is one possibility. I've worked with other projects where you create a table, and save that table as a session variable, so whatever structure you put into an object is retained if you save that object as a session variable.

array not holding value when updated

i can add a value to array(0), but when i then add a value to array(1) it clears the value for array(0). I've tried every way I can think of to declare and create the array. My code looks like this:
Dim aryEstimateInfo() As String = New String(7) {}
Private Sub wzrdEstimateWizard_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wzrdEstimateWizard.NextButtonClick
Select Case wzrdEstimateWizard.ActiveStepIndex
Case 0 'first estimate wizard step
aryEstimateInfo(0) = rad_lstVehicleType.SelectedItem.ToString
Case 1 'second estimate wizard step
Dim DamageZoneSelected As Boolean = False
For Each cntrl As Control In pnlDamageZone.Controls
If TypeOf cntrl Is RadioButton Then
Dim RadButton As RadioButton = cntrl
If RadButton.Checked Then
DamageZoneSelected = True
DamageZone = RadButton.Text.ToString
Exit For
Else
DamageZoneSelected = False
End If
End If
Next
If DamageZoneSelected = True Then
lblDamageZoneError.Visible = False
aryEstimateInfo(1) = DamageZone
Else
'if no damage zone is selected a message is displayed
wzrdEstimateWizard.ActiveStepIndex = 2
wzrdEstimateWizard.ActiveStepIndex = 1
lblDamageZoneError.Visible = True
End If
Case 2 'third estimate wizard step
'assigns the number of dents to the estimate array
aryEstimateInfo(2) = ddlNumberOfDents.SelectedValue.ToString
'sets the average dent size in the estimate arrau
aryEstimateInfo(3) = ddlDentSize.SelectedValue.ToString
'sets the add-on code and number of oversized dents
If ddlOverSized.Enabled = True Then
'aryEstimateInfo.SetValue("3", 4)
aryEstimateInfo(4) = "3"
aryEstimateInfo(7) = ddlOverSized.SelectedValue.ToString
Else
End If
Case 3 'fourth estimate wizard step
Case Else
End Select
End Sub
I'm using this in an ASP.Net wizard control and in basic, visual studio 2010.
The problem is that each button click is posting back the page, which causes your aryEstimateInfo to be re-created on each postback.
In order to handle this situation elegantly, improve the maintenance of the page, and make it easier to debug this sort of situation in the future, I recommend the following changes:
1) Change the array to a class with properties:
Public Class EstimateInfo
Public VehicleType As String = ""
Public DamageZone As String = ""
Public NumberOfDents As String = ""
Public DentSize As String = ""
Public AddOnCode As String = ""
Public Oversized As String = ""
End Class
Note that all of the properties are declared as string, but the data types should probably be changed to more accurately reflect the underlying content.
This approach will help debugging because you can change the auto-implemented property to a getter/setter so that you can place a breakpoint to see where the value is getting cleared:
Private m_sVehicleType As String = ""
Public Property VehicleType As String
Get
Return m_sVehicleType
End Get
Set (Value As String
' You could set a breakpoint here to discover where the value is getting cleared.
m_sVehicleType = Value
End Set
End Property
And if you need to have the values in a string array for export to a different application or database, for example, you could add a method to the class to produce an appropriate string array.
2) Add a property to the page to store the current answer class in the page's ViewState so that you won't have to continuously re-populate the array. For example:
Private Property EstimateInfo As EstimateInfo
Get
' Add a new record to the viewstate if it doesn't already exist
If ViewState("EstimateInfo") Is Nothing Then
Me.EstimateInfo = New EstimateInfo
End If
Return ViewState("EstimateInfo")
End Get
Set (value As EstimateInfo)
ViewState("EstimateInfo") = value
End Set
End Property
Once you do this, your wizard code becomes much easier to understand and maintain:
Select Case wzrdEstimateWizard.ActiveStepIndex
Case 0 'first estimate wizard step
Me.EstimateInfo.VehicleType = rad_lstVehicleType.SelectedItem.ToString
when you declare the new array someehere in the code you cannot reuse it again after post back.
I suggest to build the array on finish wizard event
you can use the controls in whole steps where ever step you in
I guess it will be fine
otherwise you need to store the array after every update in session or view state but I don't like both
sorry I couldn't view example becoz I'm using mobile

Property getting incorrect value set

I have a bizarre problem. I use a lot of session variables so I don't have to pass it all back and forth every time a page does a postback. I have been doing this for years so I'm at a complete loss.
I have a file called SessionHandler.vb in the App_Code folder that has the following code:
Imports Microsoft.VisualBasic
Public Class SessionHandler
Private Shared _chgLinePkNum As String = "0"
Private Shared _chgStudentIDPk As String = "0"
Public Shared Property chgLinePkNum() As String
Get
' Check for null first
If (HttpContext.Current.Session(SessionHandler._chgLinePkNum) Is Nothing) Then
' Return an empty string if session variable is null.
Return "Nothing"
Else
Return HttpContext.Current.Session(SessionHandler._chgLinePkNum).ToString()
End If
End Get
Set(ByVal value As String)
If (value Is Nothing) Or (value = "") Then
HttpContext.Current.Session(SessionHandler._chgLinePkNum) = "Nothing"
Else
HttpContext.Current.Session(SessionHandler._chgLinePkNum) = value
End If
End Set
End Property
Public Shared Property chgStudentIDPk() As String
Get
' Check for null first
If (HttpContext.Current.Session(SessionHandler._chgStudentIDPk) Is Nothing) Then
' Return an empty string if session variable is null.
Return "Nothing"
Else
Return HttpContext.Current.Session(SessionHandler._chgStudentIDPk).ToString()
End If
End Get
Set(ByVal value As String)
If (value Is Nothing) Or (value = "") Then
HttpContext.Current.Session(SessionHandler._chgStudentIDPk) = "Nothing"
Else
HttpContext.Current.Session(SessionHandler._chgStudentIDPk) = value
End If
End Set
End Property
Simple enough... Then, in my code, I reference the properties by SessionHandler.chgLinePkNum. This block of code has LineItemNumber = 1 and StudentID = [the actual ID number].
If IsParking And checkbox.Checked = True Then
SessionHandler.chgLinePkNum = LineItemNumber
SessionHandler.chgStudentIDPk = StudentID
peParkingRegistration.Show()
End If
When the first line runs, chgLinePkNum is set to 1 as expected. For some strange reason, it is also setting chgStudentIDPk to 1. When the next line is run, it sets chgStudentIDPk to the correct StudentID number. The problem is, it also sets chgLinePkNum to the StudentID number.
I have run it line by line in the debugger and each property set function runs only when it is called. I just can't figure out how "HttpContext.Current.Session(SessionHandler._chgLinePkNum) = value" is setting the value for chgStudentIDPk and vice versa.
Anything to do with these having the exact same value?
Private Shared _chgLinePkNum As String = "0"
Private Shared _chgStudentIDPk As String = "0"

Resources