How can I add an inline VB.NET method? - asp.net

Since I'm unable to add a .vb code-behind file to my .aspx page, as delineated here, I'm trying to add the method "inline" like so (the "ConvertSubcategoryIndex" function is the new bit of code):
<%
. . .
If Request.Form.Item("Action") = "Save" Then
. . .
.Parameters.Add("#Subcategory", SqlDbType.NVarChar).Value = ConvertSubcategoryIndex(Category, Subcategory)
. . .
End If 'If Save Action
Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
If _Category = "New"
If _Subcategory = 0
Return "New"
End If
Else If _Subcategory = 1
Return "Assumed"
End If
End If
If _Category = "Existing"
If _Subcategory = 0
Return "Existing"
End If
Else If _Subcategory = 1
Return "Organic"
End If
End If
Return "Unknown"
End Function
%>
That doesn't work, though, and I get this:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.
Source Error:
Line 274: End If 'If Save Action
Line 275:
Line 276: Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
Line 277: If _Category = "New"
Line 278: If _Subcategory = 0
Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx Line: 276
NOTE: I get this whether I put the function above or below End If 'If Save Action
UPDATE
Based on somebody's alias (NoAlias), I tried adding a section at the top with the code like so:
. . .
</head>
<%
Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
If _Category = "New"
If _Subcategory = 0
Return "New"
End If
Else If _Subcategory = 1
Return "Assumed"
End If
End If
If _Category = "Existing"
If _Subcategory = 0
Return "Existing"
End If
Else If _Subcategory = 1
Return "Organic"
End If
End If
Return "Unknown"
End Function
%>
<%
Unit = Request.QueryString.Item("Unit")
. . .
...but it made no difference; I still get, "Statement cannot appear within a method body. End of method assumed."
UPDATE 2
Even with the improved code from tgolisch:
Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
If _Category = "New" Then 'really need a "Then" for every "If"
If _Subcategory = 0 Then
Return "New"
'End If 'this End If was killing the next line
ElseIf _Subcategory = 1 Then
Return "Assumed"
End If
End If
If _Category = "Existing" Then
If _Subcategory = 0 Then
Return "Existing"
'End If 'this one was causing problems too
ElseIf _Subcategory = 1 Then 'ElseIf can't have a space between Else If
Return "Organic"
End If
End If
Return "Unknown"
End Function
...I get the same err msg as before:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.
Source Error:
Line 63: <%
Line 64: 'End Sub
Line 65: Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
Line 66: If _Category = "New" Then 'really need a "Then" for every "If"
Line 67: If _Subcategory = 0 Then
Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx Line: 65
Weirder yet, Visual Studio finds a problem with the first line of the code, and makes the bizarre suggestion that I prepend an "End Sub" there:
I tried that, just for yuks, but as might be suspected, that raised an err of its own, elsewhere.
UPDATE 3
Based on both answers (tgolisch's fixing the code itself, and Sam Axe's showing how it needs to be added to the mix), it is now working. With those fixes and fixes to my logic, in its working state it is:
<script runat="Server">
Function ConvertSubcategoryIndex(_Category As String, _Subcategory As String) As String
If _Category = "New Business" Then
If _Subcategory = "0" Then
Return "New"
ElseIf _Subcategory = "1" Then
Return "Assumed"
End If
End If
If _Category = "Existing Business" Then
If _Subcategory = "0" Then
Return "Existing"
ElseIf _Subcategory = "1" Then
Return "Organic"
End If
End If
Return "Unknown"
End Function
</script>

I haven't done WebForms in ages... but if memory serves you need to use something like:
<script runat="Server">
Public Function Whatever() As String
Return "something"
End Function
</script>

Your VB.NET syntax had several syntax errors. Try pasting this code for your ConvertSubcategoryIndex() function:
<%
Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
If _Category = "New" Then 'really need a "Then" for every "If"
If _Subcategory = 0 Then
Return "New"
'End If 'this End If was killing the next line
ElseIf _Subcategory = 1 Then
Return "Assumed"
End If
End If
If _Category = "Existing" Then
If _Subcategory = 0 Then
Return "Existing"
'End If 'this one was causing problems too
ElseIf _Subcategory = 1 Then 'ElseIf can't have a space between Else If
Return "Organic"
End If
End If
Return "Unknown"
End Function
%>
You also might need to take a look at the quality of the code at the top of your first example. I can't tell what is happening where you put ....

Related

Convert VB code after framework update

The framework has recently been updated on my computer and I've updated from Visual Studio 2008 to 2010, now there is a part of my code that won't work.
Public Property ItemCount() As Integer
Get
Dim val As Object = ViewState("ItemCount")
Return If(val IsNot Nothing, CInt(val), 0)
End Get
Set(ByVal value As Integer)
ViewState("ItemCount") = value
End Set
End Property
The "Return If(val IsNot Nothing, CInt(val), 0)" part of the code does not work
ERROR:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30201: Expression expected.
Source Error:
Line 21: Get
Line 22: Dim val As Object = ViewState("ItemCount")
Line 23: Return If(val IsNot Nothing, CInt(val), 0)
Line 24: End Get
Line 25: Set(ByVal value As Integer
Is there a converter that I can use to bring this part of the code upto date, I'm assuming is the code that's now been outdated.
Thanks.
Get
Dim val As Object = ViewState("ItemCount")
' Return If(val IsNot Nothing, CInt(val), 0)
If val Is Nothing Then
Return 0
Else
Return (CInt(val))
End If
End Get
Try This, it should work.

XSS Shell error: Variable "fm_QNSTR" not defined

The error while executing the file is:
VARIABLE fm_QNSTR not defined
Here's the part of the code. fm_QNSTR is on the 2nd line only.
'// Password protected pages
Sub protected()
'XSS Shell Proxy Check
If fm_Qnstr("XSSSHELLPROXY") > 0 Then
Response.Write 13
Response.End
End If
Dim ThisPage
ThisPage = Server.HtmlEncode(Request.ServerVariables("SCRIPT_NAME"))
Dim Pass
Pass = Request.Form("pass")
If Len(Pass) = 0 Then Pass = Request.Querystring("pass")
'// Set Session + password is Case Sensitive
If Pass <> "" Then
If Trim(Pass) = "w00t" Then Session("level") = "ok"
'Response.Redirect ""
End If
'// Logout (xxx.asp?logout=ok)
If Request.Querystring("logout") <> "" Then Session("level") = ""
fm_Qnstr is not a vbscript or intrinsic function. You must define it. Googling it, it appears that it needs to be
Function fm_QNStr(byVal Qstring)
Qstring= Trim(Request.Querystring(Qstring))
If NOT IsNumeric(Qstring) Then fm_QNStr = 0 Else fm_QNStr = Qstring
End Function

Function Error Object Required 800a01a8

I'm relatively new to functions and classes so not too sure if this is a beginners mistake. I'm getting:
Microsoft VBScript runtime error '800a01a8'
Object required: 'EngineerNote(...)'
/backup-check/backup_frontendlist_import_NEW.asp, line 76
Line 76 is:
Set NoteArray=EngineerNote(company, servername, backupsolution)
The three variables I'm passing are all strings. All the function and class is set in:
Class EngineerNoteClass
public note
public notesubmitdate
End Class
Function EngineerNote(Company, ServerName, Solution)
Set RecordSet = Server.CreateObject("ADODB.Recordset")
RecordSetSQLString = "SELECT note, submitdate FROM tbl_BackupChecks_AuditInformation WHERE Company='" & company & "' AND ServerName='" & servername & "' AND Solution='" & solution & "' ORDER BY submitdate desc;"
RecordSet.Open RecordSetSQLString, DatabaseConnection
If Recordset.EOF Then
'Do Nothing
Else
Dim NoteResults
Set NoteResults = new EngineerNoteClass
noteresults.note = RecordSet("note")
noteresults.notesubmitdate = RecordSet("submitdate")
Set Engineernote = NoteResults
End If
Recordset.Close
End Function
Most likely your database query didn't return any results. You only set the return value of your function when the recordset isn't at EOF:
If Recordset.EOF Then
'Do Nothing
Else
...
Set Engineernote = NoteResults
End If
Setting the return value to Nothing (or to an empty EngineerNoteClass object) in the Then branch should make the error go away:
If Recordset.EOF Then
Set EngineerNote = Nothing
Else
...
Set Engineernote = NoteResults
End If
Make sure you handle the returned value/object appropriately in the rest of your script.

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"

Display recapacha after 3 invalid login attempts?

Private Sub CheckLogin()
If Failed == 3
'show recapacha
If Page.IsValid Then
CheckLogin2()
End If
End If
Else
Try
'login code
' on password fail Failed+1
End Try
End If
What I have is a login form but I do not want to show the recapacha till there are 3 invalid login attempts. I'm not sure as to the best way to do that. Above is some mockup code to get an idea on where I am heading.
Private Sub CheckLogin()
'after the three attempts
If (LoginCount = 3) Then
'show captcha code
Return
End If
'your login code if found unsuccessful increase the counter
LoginCount += 1
End Sub
Property to increase the counter and check the number of login
attempts
Public Property LoginCount() As Integer
Get
If (ViewState("LoginCount") = Nothing) Then
ViewState("LoginCount") = 0
End If
Return DirectCast(ViewState("LoginCount"), Integer)
End Get
Set(ByVal value As Integer)
ViewState("LoginCount") = value
End Set
End Property

Resources