Link to home
Start Free TrialLog in
Avatar of Member_2_5230414
Member_2_5230414

asked on

Reference to a non-shared member requires an object reference

I get Reference to a non-shared member requires an object reference for this

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim testpage As String = "test"

        trackusers(testpage)
    End Sub

Open in new window


Imports Microsoft.VisualBasic
Imports System.Data.OleDb
Imports System.Data

Public Class trackuser

    Public Function trackusers(ByVal thepage As String) As String
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
        Dim cmd As New OleDbCommand
        cmd.Connection = con
        Try
            con.Open()

            cmd.CommandText = "INSERT into nousersonline (Username,Activity,PageUrl,ActivityDate) VALUES (test1,test2,test3,test4)"




        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
        Return ""
    End Function
End Class

Open in new window



All im tyring to do is track users on certain pages of my forum.
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

You need to instantiate the object before you can call methods from it. Alternately, you can mkae the method shared, so that you don't need to do this.
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim testpage As String = "test"
Dim trackUsers as New trackuser
        trackUsers.trackusers(testpage)
    End Sub


OR

Public Class trackuser

    Public Shared Function trackusers(ByVal thepage As String) As String
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
        Dim cmd As New OleDbCommand
        cmd.Connection = con
        Try
            con.Open()
            cmd.CommandText = "INSERT into nousersonline (Username,Activity,PageUrl,ActivityDate) VALUES (test1,test2,test3,test4)"
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
        Return ""
    End Function
You might also want to consider using the "Using" statement to ensure your resources are properly disposed.

    Public Shared Sub trackusers(ByVal thepage As String)
        Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
            Using cmd As New OleDbCommand("INSERT into nousersonline (Username,Activity,PageUrl,ActivityDate) VALUES (test1,test2,test3,test4)", con)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End Sub

You  may notice that this code doesn't worry about the Finally block you had; when End Using is called, it takes care of calling Dispose on the object, which will close the connection for you.

Also, I took the liberty of adding the ExecuteNonQuery statement so that the code actaully executes the SQL statement you'd specified.
Avatar of Member_2_5230414
Member_2_5230414

ASKER

I ran the above code and i get "no value given for one or more required paramenter on cmd.ExecuteNonQuery()
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial