use Application variable in asp to asp.net - asp.net

i am converting a old application from asp classic to ASP.net VB. i find a code part in classic version where application variable is declared and i want to use this in my ASP.NET in Globel.asax file. I am getting confuse how i use this in my ASP.NET in globel.asax
ASP Classic Code
if application("noticeupdate")="OPEN" or application("noticeupdate")="CLOSED" then
else
application("noticeupdate")="OPEN"
end if
ASP.NET
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
'Dim
If Application("noticeupdate") = "OPEN" Or Application("noticeupdate") = "CLOSED" Then
Else
Application("noticeupdate") = "OPEN"
End If
End Sub
Can you help me how can i convert this function .
Thanks for your reply

This is the syntax for same thing, I am not really understanding your code however...
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
if application("noticeupdate")="OPEN" or application("noticeupdate")="CLOSED" then
...
else
application("noticeupdate")="OPEN"
end if
End Sub
You can read more here regarding application state:
http://msdn.microsoft.com/en-us/library/ms178594(v=vs.100).aspx
And on the page you can do something like this to display the value:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = application("noticeupdate")
'or Response.Write application("noticeupdate")
End Sub

Related

VB.NET display label text only on default.aspx page only

Have a message that currently displays on every page of our website. I'd like to change this so that it only appears on the homepage which is default.aspx. Please let me know what I can try. the below code lives in the code behind of the master page of the site. We use vb.net but if someone can write it in C# I can convert it.
Previous code that displays label text on every page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End Using
End Sub
Code I have written that isn't working, what am I missing?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
Dim appPath As String = Request.PhysicalApplicationPath
If appPath = "/Default.aspx" Then
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End If
End Using
End Sub
Ended up wrapping the control in a panel with ID of Message and using the following code to display only if on the homepage.
If Page.Title = "Home" Then
Message.Visible = True
Else
Message.Visible = False
End If

How do i keep a record/count how many people visit my site?

Hi every one I have simple web app published on Internet Click to see that count the number of visitors and it's work fine with that but the problem is the counter is keep reset to default NO.0 I want the counter to keep it's value without reset and my code in Global.asax is
<%# Application Language="VB" %>
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
' Code that runs on application startup
Application("SiteVisitedCounter") = 5000
'to check how many users have currently opened our site write the following line
Application("OnlineUserCounter") = 0
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Application.Lock()
Application("SiteVisitedCounter") = Convert.ToInt32(Application("SiteVisitedCounter")) + 1
'to check how many users have currently opened our site write the following line
Application("OnlineUserCounter") = Convert.ToInt32(Application("OnlineUserCounter")) + 1
Application.UnLock()
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
Application.Lock()
Application("OnlineUserCounter") = Convert.ToInt32(Application("OnlineUserCounter")) - 1
Application.UnLock()
End Sub
and In the Code Behind is
Label3.Text = Application("SiteVisitedCounter").ToString()
Label4.Text = Application("OnlineUserCounter").ToString()

Aspx page deployment on IIS V7 Postback redirect to another page doesn't work

I Have 2 Pages of ASP V4.5 which are Login.aspx as a starting page and contentMainpage.aspx.
It's working when I've tested on debug mode in visual studio 2012, but when I try to deploy it on IIS the button log in is not responding redirect to another page.
The problem can be on web configuration
I try to google it for 2 days and can not get the solution right.
Please help.
here the code bellow:
Imports PurchaseOrderList.Class1
Public Class Login
Inherits System.Web.UI.Page
Dim abmPO As New ServiceReference1.Service1SoapClient
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("LoginName") = ""
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If IsPostBack Then
Dim LoginAutentif As New PurchaseOrderList.Class1
If (LoginAutentif.ValidatePassword(Trim(TxtLoginName.Text), Trim(TxtPassW.Text))) = 0 Then
LblErrorMessage.Visible = True
LblErrorMessage.Text = "Invalid User Name or User Password"
Exit Sub
Else
If (Response.IsClientConnected) Then
Response.Redirect("~/PurchaseOrderHeaders.aspx", False)
Session("LoginName") = TxtLoginName.Text
End If
End If
End If
Catch ex As Exception
End Try
End Sub
End Class
Maybe try to "return Response.Redirect". I had this problem in MVC5.
Your issue is most likely to do with this line:
If (Response.IsClientConnected) Then
Quick google suggests there may be a few issues with IIS7/7.5 and using this property. Also I don't think it's really necessary here, it would make more sense being used in a long running routine to detect a client disconnect or timeout.
Try changing you button click to the following and see how you go.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If IsPostBack Then
Dim LoginAutentif As New PurchaseOrderList.Class1
If (LoginAutentif.ValidatePassword(Trim(TxtLoginName.Text), Trim(TxtPassW.Text))) = 0 Then
LblErrorMessage.Visible = True
LblErrorMessage.Text = "Invalid User Name or User Password"
Exit Sub
Else
Session("LoginName") = TxtLoginName.Text
Response.Redirect("~/PurchaseOrderHeaders.aspx", True)
End If
End If
Catch ex As Exception
End Try
End Sub

Global.asax breaks with AJAX Control Toolkit

Everything was working fine. Then I added the Global.asax and suddenly got this error:
Line: 4723
Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'eeCtrl_Data = null;|
<%# Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
HttpContext.Current.Items("renderStartTime") = DateTime.Now
End Sub
Protected Sub Application_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs)
Dim startTime As DateTime = CType(HttpContext.Current.Items("renderStartTime"), DateTime)
Dim renderTime As TimeSpan = DateTime.Now - startTime
HttpContext.Current.Response.Write("<!-- Render Time (in milliseconds): " & renderTime.TotalMilliseconds.ToString & " -->")
End Sub
Try commenting out this line and see if that fixes it.
HttpContext.Current.Response.Write("<!-- Render Time (in milliseconds): " & renderTime.TotalMilliseconds.ToString & " -->")
HTH.
This is not elegant, but it works for my needs (thankfully this is an internal app). It's a two-part solution. One is for the postbacks that occur due to an UpdatePanel postback. The other is for regular postback.
I'd rather not close this thread, because I think better solutions can be presented.

ASP.Net page methods execution time monitoring

could anybody advice me, are there any approaches to calculate the methods execution time in asp.net page?
ASP.Net Trace doesn't suit me since it ignores ajax partial postbacks though just they are the tight place.
I have the page event which for some unknown reason executes slowly. Since the logic and event execution chain is rather complicated, I still unable to merely determine the culprit in debugger.
I'd like to measure the execution time of each method throughout the page, but I still see no way but insert redundant code lines (like timeBegin = DateTime.Now.... timeEnd=DateTime.Now = timeBegin etc) in the beginning and ending of each method. It seems to be ugly approach, does anybody know how to do it automatically? Are there tools which measure the methods execution time available?
I am not 100% sure the below works in all cases. So far it does though. Note that Context.Session.Item("CURRENT_WEB_USER") is where I store the current user, and is therefore not readily available as part of the asp.net session variables.
The code hereunder is in replacement of Global.aspx that allows both full page timing and timing of developer code.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_AcquireRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If HttpContext.Current.Session Is Nothing Then Return
If TypeOf HttpContext.Current.CurrentHandler Is Page Then
If Not Context.Items.Contains("Request_Start_Time") Then
Dim MyPage As Page = HttpContext.Current.CurrentHandler
AddHandler MyPage.Init, AddressOf PageInitEncountered
AddHandler MyPage.Unload, AddressOf PageUnloadEncountered
Context.Items.Add("Request_Start_Time", DateTime.Now)
Dim User As String = Context.Session.Item("CURRENT_WEB_USER")
Context.Items.Add("Request_User", User )
End If
End If
End Sub
Sub Application_ReleaseRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If TypeOf Context.CurrentHandler Is Page Then
Dim Page As Page = Context.CurrentHandler
Dim StartDate As Date = Context.Items("Request_Start_Time") : If StartDate = Nothing Then Return
Dim EndDate As Date = Now
Dim StartProc As Date = Context.Items("Page_Init")
Dim EndProc As Date = Context.Items("Page_Unload")
Dim User As String = Context.Items("Request_User")
LogEntry(StartDate, EndDate, StartProc, EndProc, User, Context.Request.Url.ToString)
End If
End Sub
Public Sub PageInitEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Init", DateTime.Now)
End Sub
Public Sub PageUnloadEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Unload", DateTime.Now)
End Sub
Public Sub LogEntry(StartDate As Date, EndDate As Date, StartProc As Date, EndProc As Date, User As String, PageURL As String)
System.Diagnostics.Debug.WriteLine(" (" & LogEntry.RequestMs & " - " & LogEntry.DurationMs & ") (" & User & ") " & PageURL)
End Sub
This may not be what you are looking for, but RedGate has a product called Performance Profiler that does exactly that. They do have a 15 day trial so you can even download it and try it out on your current issue. IMHO, it's worth every penny.
You can use asp.net performance counters and other things also. But that will not measure a page speed but they will measure a over all application performance.

Resources