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
All im tyring to do is track users on certain pages of my forum.
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
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
All im tyring to do is track users on certain pages of my forum.
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\runningprofi les\forums \forum.mdb ;")
Using cmd As New OleDbCommand("INSERT into nousersonline (Username,Activity,PageUrl ,ActivityD ate) 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.
Public Shared Sub trackusers(ByVal thepage As String)
Using con As New OleDbConnection("Provider=
Using cmd As New OleDbCommand("INSERT into nousersonline (Username,Activity,PageUrl
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.
ASKER
I ran the above code and i get "no value given for one or more required paramenter on cmd.ExecuteNonQuery()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(test
End Sub
OR
Public Class trackuser
Public Shared Function trackusers(ByVal thepage As String) As String
Dim con As New OleDbConnection("Provider=
Dim cmd As New OleDbCommand
cmd.Connection = con
Try
con.Open()
cmd.CommandText = "INSERT into nousersonline (Username,Activity,PageUrl
Catch ex As Exception
Throw ex
Finally
con.Close()
End Try
Return ""
End Function