Help making code reusable

K

Kirk Groome

Hello All,

I'm just learning asp.net and I have a problem so I hope some one can please help me out. I have some code that works in the VB file but I want to be able to use of more then one page. I have tried to make it a public class and I get the following error.


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: BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Source Error:


Line 20: 'The doce below here is waht I would like o put in
Line 21: StrConn = ConfigurationManager.ConnectionStrings("ADO_SQL").ConnectionString
Line 22: dbWebSQL = Server.CreateObject("ADODB.Connection")
Line 23: dbWebSQL.Open(StrConn)
Line 24: rstWebTable = Server.CreateObject("ADODB.Recordset")


The code in my VB file is:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim StrConn As String
Dim dbWebSQL 'As server.ADODB.Connection
Dim rstWebTable 'As ADODB.Recordset

If User.Identity.IsAuthenticated Then
Dim myUser As MembershipUser = Membership.GetUser
Session("Username") = myUser.UserName.ToString()
tUserName.Text = Session("Username")
End If

'The Code below here is waht I would like to reuse on other pages.
StrConn = ConfigurationManager.ConnectionStrings("ADO_SQL").ConnectionString
dbWebSQL = Server.CreateObject("ADODB.Connection")
dbWebSQL.Open(StrConn)
rstWebTable = Server.CreateObject("ADODB.Recordset")
rstWebTable.LockType = 2
rstWebTable.open("Select * from WebTracking", dbWebSQL)
With rstWebTable
.Addnew()
.Fields("TrackingObject") = "Secure Web Page"
.Fields("TrackingDate") = DateTime.Now
.Fields("TrackingUser") = Session("username")
.Fields("TrackingPage") = Request.ServerVariables("PATH_INFO")
.Fields("TrackingIP") = Request.ServerVariables("REMOTE_ADDR").ToString
.Update()
.Close()
End With
dbWebSQL.Close()


End Sub

Here is what I have in the MM4class.vb file

Public Class MM4Class

Inherits System.Web.UI.Page

Public Shared Sub TrackPageUsage()


Dim StrConn As String

Dim dbWebSQL 'As server.ADODB.Connection

Dim rstWebTable 'As ADODB.Recordset

'The doce below here is waht I would like o put in

StrConn = ConfigurationManager.ConnectionStrings("ADO_SQL").ConnectionString

dbWebSQL = Server.CreateObject("ADODB.Connection")

dbWebSQL.Open(StrConn)

rstWebTable = Server.CreateObject("ADODB.Recordset")

rstWebTable.LockType = 2

rstWebTable.open("Select * from WebTracking", dbWebSQL)

With rstWebTable

..Addnew()

..Fields("TrackingObject") = "Secure Web Page"

..Fields("TrackingDate") = DateTime.Now

..Fields("TrackingUser") = Session("username")

..Fields("TrackingPage") = Request.ServerVariables("PATH_INFO")

..Fields("TrackingIP") = Request.ServerVariables("REMOTE_ADDR").ToString

..Update()

..Close()

End With

dbWebSQL.Close()

End Sub

End Class
 
C

Cor Ligthert[MVP]

Kirk,

The code you are using (Ado) is from about 1998 far from the time there was AdoNet where this newsgroup is about.

Try to learn something about AdoNet, just simple type it in Google and you get millions of pages leading you in the right direction.

Cor

"Kirk Groome" <[email protected]> schreef in bericht Hello All,

I'm just learning asp.net and I have a problem so I hope some one can please help me out. I have some code that works in the VB file but I want to be able to use of more then one page. I have tried to make it a public class and I get the following error.


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: BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Source Error:


Line 20: 'The doce below here is waht I would like o put in
Line 21: StrConn = ConfigurationManager.ConnectionStrings("ADO_SQL").ConnectionString
Line 22: dbWebSQL = Server.CreateObject("ADODB.Connection")
Line 23: dbWebSQL.Open(StrConn)
Line 24: rstWebTable = Server.CreateObject("ADODB.Recordset")


The code in my VB file is:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim StrConn As String
Dim dbWebSQL 'As server.ADODB.Connection
Dim rstWebTable 'As ADODB.Recordset

If User.Identity.IsAuthenticated Then
Dim myUser As MembershipUser = Membership.GetUser
Session("Username") = myUser.UserName.ToString()
tUserName.Text = Session("Username")
End If

'The Code below here is waht I would like to reuse on other pages.
StrConn = ConfigurationManager.ConnectionStrings("ADO_SQL").ConnectionString
dbWebSQL = Server.CreateObject("ADODB.Connection")
dbWebSQL.Open(StrConn)
rstWebTable = Server.CreateObject("ADODB.Recordset")
rstWebTable.LockType = 2
rstWebTable.open("Select * from WebTracking", dbWebSQL)
With rstWebTable
.Addnew()
.Fields("TrackingObject") = "Secure Web Page"
.Fields("TrackingDate") = DateTime.Now
.Fields("TrackingUser") = Session("username")
.Fields("TrackingPage") = Request.ServerVariables("PATH_INFO")
.Fields("TrackingIP") = Request.ServerVariables("REMOTE_ADDR").ToString
.Update()
.Close()
End With
dbWebSQL.Close()


End Sub

Here is what I have in the MM4class.vb file

Public Class MM4Class

Inherits System.Web.UI.Page

Public Shared Sub TrackPageUsage()


Dim StrConn As String

Dim dbWebSQL 'As server.ADODB.Connection

Dim rstWebTable 'As ADODB.Recordset

'The doce below here is waht I would like o put in

StrConn = ConfigurationManager.ConnectionStrings("ADO_SQL").ConnectionString

dbWebSQL = Server.CreateObject("ADODB.Connection")

dbWebSQL.Open(StrConn)

rstWebTable = Server.CreateObject("ADODB.Recordset")

rstWebTable.LockType = 2

rstWebTable.open("Select * from WebTracking", dbWebSQL)

With rstWebTable

.Addnew()

.Fields("TrackingObject") = "Secure Web Page"

.Fields("TrackingDate") = DateTime.Now

.Fields("TrackingUser") = Session("username")

.Fields("TrackingPage") = Request.ServerVariables("PATH_INFO")

.Fields("TrackingIP") = Request.ServerVariables("REMOTE_ADDR").ToString

.Update()

.Close()

End With

dbWebSQL.Close()

End Sub

End Class
 
Top