I have been racking my brain for days on this and have flipped through every ASP.Net book i have and have yet to come up with the correct way to do this! I am writing a custom class to make my code more manageable (obviously) but am having some difficulty.
Here is the code:
Code:
Public Class productImage
Private intId as integer 'Value for productID
Private strImagePath as string 'String Value for the Image Path
Public Function GetImage(ByVal intPID as integer)
Dim objConnection as New OleDbConnection 'Dbase Connection
Dim objCommand as New OleDbCommand 'Dbase Command
Dim objDataReader as OleDbDataReader 'DataReader
Dim strSQLQuery as string = "" 'SQL Query
objConnection = New System.Data.OLEDB.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("./dbase/schools.mdb") & ";" )
objConnection.open()
strSQLQuery = "SELECT * from images where pid=" &intPID
objCommand = New OleDbCommand(strSQLQuery, objConnection)
objDataReader = objCommand.ExecuteReader()
While objDataReader.Read
'lblText.text ="You selected a " & objDataReader("prod_desc")
SetImage("<img src=" & objDataReader("prod_image") &">")
End While
objConnection.Close()
objDataReader.Close()
objDataReader = Nothing
objConnection = Nothing
End Function
Public Property SetImage() as string
Get
return strImagePath
End Get
Set(ByVal imageString as String)
strImagepath = imageString
End Set
End Property
End Sub
End Class
Originaly I had the objConnection, DataReader, and command defined as private with all of my other variables because i was getting the error:
Reference to a non-shared member requires an object reference. on line 17 where i declare
Code:
objConnection = New System.Data.OLEDB.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("./dbase/schools.mdb") & ";" )
I then moved the variables into the function thinking, for some reason, i couldnt access them. (Didnt make sense but I was working on process of elimination) again i got the same error. Then I thought that maybe I couldnt access the namespace so typed out the exact location of, for example, the OleDbConnection.
I also am getting an error where i try to call my property:
Code:
SetImage("<img src=" & objDataReader("prod_image") &">")
saying that to access a property i must either use or set its value.
Any ideas??
'Edit
I went back through the code and replaced Server.MapPath([location]) with the direct path to the database and this resolved the error, does anyone know why this would not work? I have verified that the path is correct ./dbase/schools.mdb
'Edit 2
I resolved this issue, apparently inside a class you must reference server.mappath() directly by using
Code:
httpContext.Current.Server.MapPath()
"There is nothing more honorable then doing a thing well"