• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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.
0
runnerjp2005
Asked:
runnerjp2005
  • 3
1 Solution
 
Jeff CertainCommented:
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
0
 
Jeff CertainCommented:
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.
0
 
runnerjp2005Author Commented:
I ran the above code and i get "no value given for one or more required paramenter on cmd.ExecuteNonQuery()
0
 
Jeff CertainCommented:
ah.... you may need to fix your query by putting single quotes around the values you're inserting.

            Using cmd As New OleDbCommand("INSERT into nousersonline (Username,Activity,PageUrl) VALUES ('test1','test2','test3')", con)

You'll notice I took out the date field. That's me being lazy -- I can never remember the string format for SQL Server dates.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now