How can I add an inline VB.NET method?

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:

enter image description here

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>

Upvotes: 1

Views: 1914

Answers (3)

user23072840
user23072840

Reputation:

I had the same problem now, when I wanted to display date values (from UNIX timestamps) with leading zeros in pure VBScript. I defined my Function to do some formatting, but also received BC30289 compiler message. Then I red the full compiler output, drank a coffe, and...



    Dim ts, td, t
    ts = "1690008848"
    td = DateAdd("s", ts, "01/01/1970 00:00:00")
    t = DatePart("yyyy", td) & "-"
    t = t & DatePartFormatter(td, "m") & "-"
    t = t & DatePartFormatter(td, "d") & " "
    t = t & DatePartFormatter(td, "h") & ":"
    t = t & DatePartFormatter(td, "n") & ":"
    t = t & DatePartFormatter(td, "s")
    Response.Write(resp & ", " & t)
    
    
    ' Here comes a small trick, after examining the full compiler output
    ' ASP pages run inside a Sub started by the system; in my case, this is called as
    '  Private Sub __Render__control1(ByVal __w As System.Web.UI.HtmlTextWriter, ByVal parameterContainer As System.Web.UI.Control)
    ' Everything in your ASP page code is inside this system Sub, that is why you get the error message of
    '  "Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed."
    ' when you try to define your own Subs or Functions to make your life easier.
    ' Fortunately, VB compiler is a good partner for a trick, because it only needs an exact string at a good position.
    ' That means: we simply have to close the system-opened Sub at the end of the page code, then we can write our Subs or Functions there,
    ' and finally we open an empty Sub, so that the system can close it instead of the original one, without reclaiming about anything.
    ' And, that's it :-)
    
    ' here we close the __Render_control1 system Sub...
    End Sub
    
    ' ...declare our program pieces, ...
    Function DatePartFormatter(d as Date, part as String)
        Dim var
        var = CStr(DatePart(part, d))
        If Len(var) = 1 Then
            var = "0" & var
        End If
        DatePartFormatter = var
    End Function
    
    ' ... and finally open a Sub, but let the system close it, after reading in the page code (by the compiler) ends completely
    Sub CompletelyDummyThing
    %>

It is a bit ugly, but it works. If not, check the parent of your ASP code in the compiler output, and align to that.

Upvotes: 0

Sam Axe
Sam Axe

Reputation: 33738

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>

Upvotes: 1

tgolisch
tgolisch

Reputation: 6734

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 ....

Upvotes: 1

Related Questions