I have a problem with running my code in VB.net it show cannot refer to an instance member of a class from within a shared method when i dim and create a session under web method , Hope you help me, Thanks!

Kenneth Fuentes 0 Reputation points
2023-03-04T03:04:18.8333333+00:00
  <System.Web.Services.WebMethod(EnableSession:=True)>
    Public Shared Function GetTopItems() As List(Of TopItems)


        Dim DTEmployees As DataTable = _clsGlobal.Get1ParameterizedDatatable(JTConnectionString, "OUSRSelectByUserName", "@UserName", Membership.GetUser.UserName)
        If DTEmployees.Rows.Count > 0 Then
            Dim RowsEmployees As DataRow = DTEmployees.Rows(0)

            Session("Branch") = RowsEmployees.Item("BranchKey")
        End If

        Dim constr As String = ConfigurationManager.ConnectionStrings("JTConnectionString").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("dbo.INV1SelectTopItems", con)
                cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.Add("@PrjCode", SqlDbType.Int).Value = Session("Branch")
                    cmd.Connection = con

                Dim topitems As New List(Of TopItems)()
                con.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        topitems.Add(New TopItems() With {
                         .dscription = sdr("dscription").ToString(),
                         .Totalqty = sdr("Totalqty"),
                         .TotalPurchase = sdr("TotalPurchase"),
                         .TotalSales = sdr("TotalSales")
                                           })
                    End While
                End Using
                con.Close()
                Return topitems
            End Using
        End Using

    End Function
.NET F#
.NET F#
.NET: Microsoft Technologies based on the .NET software framework.F#: A strongly typed, multi-paradigm programming language developed by the F# Software Foundation, Microsoft, and open contributors.
95 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2023-03-11T14:55:00.36+00:00

    This might help you https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/error-messages/bc30369

    As far I understand you can also Declare and Initialize an instance like:

    Dim xGlobal as New _clsGlobal()
    Dim DTEmployees As DataTable = xGlobal.Get1ParameterizedDatatable(JTConnectionString, "OUSRSelectByUserName", "@UserName", Membership.GetUser.UserName)
    
    0 comments No comments