1

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>
Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    The function is being declared within some other function. Methods can span multiple <% %> blocks, so make sure that ConvertSubCategoryIndex is independent of any other methods, perhaps by putting it as close to the top as possible. – NoAlias Apr 10 '17 at 18:18
  • Shucks, I thought that would help. You confirmed no other unclosed methods exist in <% %> brackets in the page above where you moved it (or in any master/parent pages)? – NoAlias Apr 10 '17 at 18:41
  • No, all works fine without this new method; it's just that unfortunately this new method is wanted. I'm going to see if I can make this a SQL Trigger instead of VB code in the page. – B. Clay Shannon-B. Crow Raven Apr 10 '17 at 18:47
  • 1
    The error message is implying that you have an "End If" without a matching "If" – tgolisch Apr 10 '17 at 20:24
  • @tgolisch: I don't know VB; maybe "END IF" is bad juju prior to "ELSE IF"... – B. Clay Shannon-B. Crow Raven Apr 10 '17 at 20:26
  • Nope, I still get the same err msg with those two "END IF"s commented out. Where might the superfluous "End If" or the missing "If" be? – B. Clay Shannon-B. Crow Raven Apr 10 '17 at 20:29
  • IIRC, ASP.NET compiles these views into actual code files. `<%` gets turned into a sub. So trying to place a function within a sub is just going to fail. I know this is how MVC works. Just can't remember for sure if its how WebForms works. – Sam Axe Apr 10 '17 at 20:51

2 Answers2

1

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

tgolisch
  • 6,549
  • 3
  • 24
  • 42
1

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>
Sam Axe
  • 33,313
  • 9
  • 55
  • 89