Context.Item value is coming as null - asp.net

On a page we have submit button, on clicking it we are getting error due to m_strPageDefinition has null value. Following is the code of it for more insight. Only sometimes and only in production we are getting value of m_strPageDefinition as null, which is causing problems. Does anyone has idea why m_strPageDefinition is coming null.
Private m_strPageDefinition As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Not Context.Items("MyXmlString") = Nothing) And (Not Context.Items("mFormID") = Nothing) Then
MyXMLString = Context.Items("MyXmlString")
MyHiddenXMLString.Value = MyXMLString
End If
Else
m_strPageDefinition = MyHiddenXMLString.Value
End If
End Sub
Private Property MyXMLString()
Get
Return m_strPageDefinition
End Get
Set(ByVal value)
m_strPageDefinition = value
End Set
End Property

You should consider being more consistent how you address m_strPageDefinition. Why are you accessing is a private variable instead of always using the Property setter? e.g.
Private m_strPageDefinition As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Not Context.Items("MyXmlString") = Nothing) And (Not Context.Items("mFormID") = Nothing) Then
MyXMLString = Context.Items("MyXmlString")
MyHiddenXMLString.Value = MyXMLString
Else
MyXMLString = MyHiddenXMLString.Value
End If
Else
MyXMLString = MyHiddenXMLString.Value
End If
End Sub
Private Property MyXMLString()
Get
Return m_strPageDefinition
End Get
Set(ByVal value)
m_strPageDefinition = value
End Set
End Property
I believe your issue is coming from the missing "Else" I have included here in the Not IsPostBack statement. Forgive me for VB.NET is not a language I code in, so the format/structure might be off a bit.

I think the problem is here:
m_strPageDefinition = MyHiddenXMLString.Value
and more specifically: MyHiddenXMLString.Value is probably null.
I couldn't figure out where you declared MyHiddenXMLString, but how about making it a hidden variable (input type="hidden" .. ) on the aspx side and setting it's value when the page loads the first time.
Then you know the value will always be there and easy to access.
hth

Related

How can do postback using response.redirect?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' hdncurrpayperiod.Value = test1.ToString
hdnPayperiod.Value = test1.ToString
lblPayPeriodStartDt.Text = test1
Else
lblPayPeriodStartDt.Text = hdnPayperiod.Value
End If
End Sub
Private Sub btnnext_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnnext.Click
' hdnPayPeriodStartDt.Value = ""
Dim datenext As Date
Dim datenextpayperiod As String
datenext = DateTime.Parse(lblPayPeriodStartDt.Text)
datenextpayperiod = datenext.Add(New TimeSpan(14, 0, 0, 0)).ToString("MM/dd/yyyy")
hdnPayperiod.Value = datenextpayperiod
lblPayPeriodStartDt.Text = hdnPayperiod.Value
'hdnPayperiod.Value = lblPayPeriodStartDt.Text.ToString
Response.Redirect("TimeSystem.aspx?PayPeriodStartDate=" + HttpUtility.UrlEncode(hdnPayperiod.Value), False)
End Sub
This is a user control which has calender with next and previous buttons and a label. I get the initial date from the a user control property on initial load(not postback) and on next button click it adds 14 days and displays on label. Everything is fine until I use response.redirect which is hitting not postback and getting the property value again. How can I avoid this. I have to pass the value of date with 14 days added to it in the url but this happens only once as it is refreshing with property value every time It hits response.redirect. Please let me know if I am not clear
In Page_Load in Not IsPostBack block add another 'if' statement that checks if 'PayPeriodStartDate' is present in query string (Request.QueryString). If it is - use that value to set hidden field and label if it isn't use 'test1' value as in your code sample.

VB.net textbox return hit(enter) event

How to catch return hit (enter) on TextBox?
The following does not work. I am keep getting error "KeyPress event can't be found".
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
If TextBox.Text Is Nothing Then Return
If TextBox.Text.Length = 6 And Asc(e.KeyChar) = 13 Then
' Do something here
End If
End Sub
try deleting the text box and re-entering the code
If TextBox.Text Is Nothing Then Return
If TextBox.Text.Length = 6 And Asc(e.KeyChar) = 13 Then
' Do something here
End If
as that should work - you most likely have something messed up with the textbox name or keypress declaration code.
I just checked that code in a new text box and it worked fine.
The following code does work:
Private Sub TextBox_KeyPress(ByVal KeyAscii As Integer)
If TextBox.Text Is Nothing Then Return
If TextBox.Text.Length = 6 And KeyAscii = 13 Then
' Do something here
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(13) Then
If TextBox1.Text.Trim <> Nothing Then
'Do Something here
End If
End If

VB.net/Asp.net loading a control programatically with a public property to be set

I have a vb.net asp application where I'm loading a control (from another control on a page). I want to set a variable between the two controls when it loads.
This is where I load the control:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
phCompanies.Controls.Add(AuditControl)
End Sub
On the control that's being loaded i've exposed the item i want to change as a property
Public Property resultType() As String
Get
Return m_resultType
End Get
Set(ByVal value As String)
m_resultType = value
End Set
End Property
Basically all it is doing is setting a parameter for my table adaptor
Public Sub Load_Data()
dtblog = New dsDECorp.ClientInfoDataTable
dtblog = tablog.GetAudits(m_resultType)
For Each rClient As dsDECorp.ClientInfoRow In dtblog
CID = rClient.ClientID
ClientName = rClient.CompanyName
Next
dtblog.Dispose()
End Sub
How do I pass the parameter through the property from the first control to the second when it loads?
Thanks
Tom
I'm a C# guy so forgive me if I make any syntax errors but the following should work for you:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
Dim AuditControlType As Type = AuditControl.GetType()
Dim AuditField As FieldInfo = AuditControlType.GetField("resultType")
AuditField.SetValue(AuditControlType, "Your Value")
phCompanies.Controls.Add(AuditControl)
End Sub

User enters code into TextBox, code gets added to URL (using session)

I'm using ASP.net 4.0 VB :)
I am using a session variable to add a user entered code into the url of each page. I can get the code to show up at the end of the page's URL that my textbox is on, but what do I add to every page to make sure that the session stays at the end of every URL that person visits during their session? This is the code from the page that the user enters their user code.
Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles IBTextBoxButton.Click
Session("IB") = IBTextBox.Text
Dim IB As String = Session("IB")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ProductID.Value = Request.QueryString("id")
If Session("IB") Is Nothing Then
tab4.Visible = "False"
Else
tab4.Visible = "True"
End If
End Sub
This is what I have in the page load of one of the other pages. What else do I add to make sure that variable is added to the URL of that page?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim IB As String
IB = Session("IB")
End Sub
string url = Request.Url.ToString();
string newUrl = url + url.Contains("?") ? "&" : "?" + "ib=" + Server.UrlEncode(IBTextBox.Text);
Response.Redirect(newUrl);
return;
The approach I might use would be to create a base page class that all of your pages can inherit. The base page would then inherit the System.Web.UI.Page.
Within your base page class, create a property for IB and also handle the page load event.
In that event, check if the QueryString has the IB parameter in it. If it does, set the property to the value in the parameter.
Private _IB As String
Public Property IB() As String
Get
Return _IB
End Get
Set(ByVal value As String)
_IB = value
End Set
End Property
Public Function GetIB(ByVal url As String) As String
If Not(_IB = String.Empty) Then
If (url.Contains("?")) Then
Return "&IB=" & _IB
Else
Return url & "?IB=" & _IB
End If
Else
Return url
End If
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (String.IsNullOrEmpty(Request.QueryString("IB"))) Then
_IB = Request.QueryString("IB")
End If
End Sub
Finally in your markup you would need to place something like the following at the end of all of your links:
next page
I threw this code into the Master Page to make sure that every page knows whether or not the Session is there. Thanks for all the help everyone!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Session("IB") Is Nothing Then
IBText.Visible = True
IBTextBox.Visible = True
IBTextBoxButton.Visible = True
Else
IBText.Visible = False
IBTextBox.Visible = False
IBTextBoxButton.Visible = False
lblIB.Visible = True
lblIB.Text = "Welcome, " + Session("First_Name") + " " + Session("Last_Name")
End If
End Sub

Putting an object in session via a property in ASP.NET

This seems like it might be a bad idea, but I can't figure out why:
I have a class, cXYZ, with properties A, B and C. It also has a method 'sGetData' that loads those three properties from the database, and a method 'sSaveData' which saves it back.
class cXYZ
public property A as string...
public property B as string...
public property B as string..
public sub sGetData()...
public sub sSaveData()...
end class
A webform has the following property:
private property xyz() as cXYZ
get
return session("myXYZ")
end get
set (value as cXYZ)
session("myXYZ")=value
end set
end property
And the following events:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
if not ispostback() then
xyz=new cXYZ()
end if
end sub
Protected Sub ButtonLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLoad.Click
//Can now reference the class
txtA.text=xyz.A
txtB.text=xyz.B
txtC.text=xyz.C
end sub
Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
//Can now reference the class
xyz.A=txtA.text
xyz.B=txtA.text
xyz.C=txtC.text
xyz.sSaveData()
end sub
I can see some overhead with serializing/deserializing for each property reference- it might be worth doing this:
Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
dim localxyz as cXYZ=xyz
localxyz .A=txtA.text
localxyz .B=txtA.text
localxyz .C=txtC.text
xyz=localxyz
end sub
Other than that, views on why this is good or bad? The class is not large, it maintains the form state. Webforms suck, etc is not very useful..
I would improve that little bit:
private _xyz as cXYZ = nothing
private property xyz() as cXYZ
get
if _xyz is nothing then _xyz = TryCast(session("myXYZ"), cXYZ)
return _xyz
end get
set (value as cXYZ)
_xyz = value
session("myXYZ")=_xyz
end set
end property
I think its ok. I would addicionaly keep a variable to store the object to improve performance.
Something like this:
private _xyz as cXYZ = nothing
private property xyz() as cXYZ
get
if not _xyz is nothing then
return _xyz
else
return session("myXYZ")
end if
end get
set (value as cXYZ)
_xyz = value
session("myXYZ")=value
end set
end property
As long as your object is serializable you are OK.
Just don't hold any unmanaged object references in your session - otherwise you'll get into trouble once you move away from 'in process' session state to a web farm.

Resources