Why is “use” apparently rejected in one function, but good in another?

With this code:

Protected Function GetArgValsForCompanyName(coName As String) As String()
    Dim args(2) As String

    Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
          cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)

        con.Open()

        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName

        Using reader As SqlDataReader = cmd.ExecuteReader

            While reader.Read
                args(0) = reader.Item(0).ToString()
                args(1) = reader.Item(1).ToString()
                args(2) = reader.Item(2).ToString()
            End While

        End Using

    End Using

    Return args
End Function

... I get:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------    
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: BC30203: Identifier expected.

Source Error:

Line 94:         Dim args(2) As String
Line 95: 
Line 96:         Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
Line 97:               cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)
Line 98: 

Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_categoryadmin.aspx.vb    Line: 96     

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491 

Thus, the string "using con" is implied as a problem. I commented out most of the code just to return some random values, and it works great.

However, another “Usage” is used: the Button1_Click event handler uses the usage. Does it complain only because the button is not pressed?

Actually, when I click the button, I get an error message, but this is not about use; this is because I apparently have too many controls in the form that the button works with (in fact, thousands or labels / check boxes):

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Operation is not valid due to the current state of the object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:     

[InvalidOperationException: Operation is not valid due to the current state of the object.]
   System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded() +4198079
   System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) +60
   System.Web.HttpRequest.FillInFormCollection() +189

[HttpException (0x80004005): The URL-encoded form data is not valid.]
   System.Web.HttpRequest.FillInFormCollection() +11196408
   System.Web.HttpRequest.get_Form() +119
   System.Web.TraceContext.InitRequest() +1188
   System.Web.TraceContext.VerifyStart() +133
   System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +11307449
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +452  



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491

"" ? - [fangl, ] ?

+4
1

VB.NET . .

VB.NET, , , .
, LinqPAD 5.10 using.

, ,

Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"), _
      cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)
+5

Source: https://habr.com/ru/post/1672871/


All Articles