Phantom VB errors in an ASPX file - asp.net

I have an aspx file containing some VB code:
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="XXX.Email" %>
<%# Import Namespace="XXX.Functions" %>
<%# Import Namespace="XXX.Rtf" %>
<%# Import Namespace="XXXDBTools" %>
<%# Import Namespace="XXXDBTools.DBFunctions" %>
<%# Import Namespace="XXXStrTools.StrFunctions" %>
<script runat="server">
Dim strToResult
Dim errorMessage
Dim errorMessageFor
Dim referralEmailStr
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
...
End Sub
</script>
<html>
<head>
<title><%=Request.Cookies("State").Value%> Request a Placement</title>
...
</head>
...
</html>
Now I see compile errors on the Request.Cookies("State").Value line, for code that doesn't even exist on this page!
If I delete the line where I'm checking for the state cookie, I still get the same errors, but at the end of the Sub Page_Init I declared above.
The variables mentioned in the errors (such as myTemp and lfyCompanyInfoDSN) do not even exist on this page, so I'm perplexed as to how I could be getting these errors...

Turns out at the very bottom of the <html> tag I had some lines such as this in a script:
set drIntakeWorker = Nothing
Which is not valid because the set keyword is not allowed in VB.NET. Somehow this error was percolating up and screwing up the entire page...

Related

WebForms page can't access properties defined in its own code-behind class

I have a WebForms page like so:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="ReportsTo.aspx.vb" Inherits="MyApp.ReportsTo" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="MyApp" %>
<%# Import Namespace="MyAppDBTools" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%
Dim nextHigher As Integer? = UserLevelID
' do more stuff here
%>
</form>
</body>
</html>
And here is the code behind:
Imports MyAppDBTools
Public Class ReportsTo
Inherits System.Web.UI.Page
''' <summary>
''' The ID of the user whose supervisor is being set.
''' </summary>
''' <returns></returns>
Protected ReadOnly Property UserID As Integer
Get
Return CInt(Request("UserID"))
End Get
End Property
''' <summary>
''' The level of the user whose supervisor is being set.
''' </summary>
''' <returns></returns>
Protected ReadOnly Property UserLevelID As Integer
Get
Return DBFunctions.GetUserLevelID(UserID, SessionHelper.ConnectionString)
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
However I get compile errors when I reference the UserID and UserLevelID properties from the page:
BC30451 'UserLevelID' is not declared. It may be inaccessible due to its protection level.
Why is this? Shouldn't I be able to access properties defined in the code-behind from the page? If not, where else can I put them? A singleton class?

files name in asp.net and vb.net

I have .aspx and .vb files. The name of it are: SA_Setup.aspx and SA_Setup.vb.
The 1st 10 lines of the code in SA_Setup.aspx file is:
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
CodeFile="SA_Setup.aspx.vb" Inherits="SA_Setup" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%# MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<link id="Link2" rel="Stylesheet" href="~/Common/OrgSelfAssessment.css" type="text/css"
media="screen" runat="server" />
</asp:Content>
And the 1st 10 lines of code in SA_Setup.aspx.vb file is:
Imports System.Data
Imports GlobalModule
Partial Class SA_Setup
Inherits System.Web.UI.Page
Dim dalGM As GlobalModule
Dim dalSelfAssessment As New SelfAssessmentSetUp
Dim dalSurveyAdmin As New SurveyAdmin
Dim dalClientAdmin As New ClientAdmin
Protected Overrides Sub InitializeCulture()
Try
Dim strCul As String : strCul = Session("Culture")
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(strCul)
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(strCul)
Catch ex As Exception
Response.Redirect("~/Error/ErrorPage.aspx?type=SessionTimedOut&ui=" & Request.QueryString("ui"), True) : Exit Sub
End Try
End Sub
Now, I have changed the files name to SA_Setup_MPHLB.aspx and SA_Setup_MPHLB.aspx.vb. I am wondering what changes do I need to do in the codes above after changing the files name. As per my understanding, I have made the following changes:
In .aspx file (SA_Setup_MPHLB.aspx):
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
CodeFile="SA_Setup_MPHLB.aspx.vb" Inherits="SA_Setup_MPHLB" %>
In .aspx.vb file (SA_Setup_MPHLB.aspx.vb):
Partial Class SA_Setup_MPHLB
Just checking if the changes I have did is right or is there any place I have to do the changes ?
If you are able to successfully rebuild the project after name changes, you may not need to do anything else. If you are getting any error, it will surely suggest you what to do next.

ASP.NET Public Variable at Code Behind is not accesible in .aspx

This is a simple example of what is happening here.
Default.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<% Response.Write(sName) %>
</div>
</form>
</body>
</html>
and the code behind default.aspx.vb
Public Class _default1
Inherits System.Web.UI.Page
Public sName As String = "Jimmy"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
the error is
BC30451 'sName' is not declared. It may be inaccessible due to its protection level. base C:\Users\Jimmy\Documents\Visual Studio 2015\Projects\base\base\teste\default.aspx 12
Where is the problem ?
The biggest issue is your class _default1 is not marked as partial class.
It should rather be
Public partial Class _default1
Inherits System.Web.UI.Page
One more point is that your page directive has the property AutoEventWireup="false" and with that the way you have your page events mapped right now will not work. So set it to true rather AutoEventWireup="true"
Your #Page directive is total weird as can be seen below.
<%# Page AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
To summarize; below are the issues you should take care off
mark the class partial
set AutoEventWireup="true"
change CodeBehind property to CodeBehind="default1.aspx.vb"
Change your Inherits property to Inherits="your_namespace._default1"

How can I access objects in the code-behind of ASP.NET in the Markup?

I have the following event handling code:
Public Class Details
Inherits System.Web.UI.Page
Public veh As Vehicle
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim id As Integer = Request("id")
veh = New DataRepository().vehicles.get(id)
End Sub
End Class
And have tried to access it in my markup like so:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Added.aspx.vb" Inherits="Auto_Dealer_Website.Added" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% veh.color()%>
</asp:Content>
but the compiler won't recognize it. What am I missing? How do I reference my veh object?
Solved
Can't reference the CodeBehind without first building the project.
Add the equals sign to output the variable:
<%= veh.make %>

Parser Error in Namespace?

I have a webpage that was built using the All In One style (the VB in the page with the HTML), and I have been asked to separate them.
This is how Default.aspx starts out:
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Configuration" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.OleDb" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Globalization" %>
<%# Import Namespace="System.Security" %>
<html>
<head>
<title>View an Activity (AFE)</title>
<script language="VB" runat="server">
Private m_url1, m_url2, m_downloadText, txtSupplementPage, txtExcerpt, SearchLocation, SearchLocation2, SearchLocation3, SearchLocationOrig As String
Private myTotal1, myTotal2 As Double
Private m_credentials As CredentialCache
' Start the initial page
Protected Sub Page_Load(sender As Object, e As EventArgs)
m_credentials = New CredentialCache()
If Not IsPostBack Then
It goes on. This is just to show you what I'm doing.
I created a new, similar file called that Default2.aspx that includes the Page directive, specifies the CodeBehind file, and the namespace (Inherits). Then I took the VB code out:
<%# Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="app2._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View an Activity (AFE)</title>
Since Default.aspx did not use a Code Behind file, I named my new Code Behind file Default.aspx.vb.
I created a new _Default with Namespace app2 within this Code Behind file, and I copied all of the VB from Default.aspx into it:
Namespace app2
Public Class _Default
Inherits System.Web.UI.Page
Inherits System.Net
Inherits System.IO
Inherits System.Configuration
Inherits System.Data
Inherits System.Data.OleDb
Inherits System.Data.SqlClient
Inherits System.Globalization
Inherits System.Security
Private m_url1, m_url2, m_downloadText, txtSupplementPage, txtExcerpt, SearchLocation, SearchLocation2, SearchLocation3, SearchLocationOrig As String
Private myTotal1, myTotal2 As Double
Private m_credentials As CredentialCache
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
m_credentials = New CredentialCache()
If Not IsPostBack Then
I am trying to give enough code that everyone can see the starting points and where I am now.
I got the two new files uploaded to the web server, and then browsed to them to see how they would look.
However, now when I try to display Default2.aspx in the browser, I get this Parser Error:
Parser Error Message: Could not load type 'app2._Default'.
Source Error:
Line 1: <%# Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="app2._Default" %>
Line 2:
Line 3:
What have I messed up?
For a web site: Change CodeBehind to CodeFile.
<%# Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="app2._Default" %>
For a web application: Add the name of the web project to the Inherits attribute.
<%# Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="nameOfYourWebProject.app2._Default" %>
Also, the _Default class you provided, you can only have one Inherits, I think you meant to add them as Imports on the top of the page.

Resources