|
-
Dec 18th, 2002, 07:51 AM
#1
Thread Starter
Addicted Member
class constructors with paramaters and code behind
Hi I have written a class to run a stored procedure. When the class is instantiated I want to pass paramaters in the constructor. This works fine in VB.NET however when I move this to ASP.net into a code behind file I get the error message:
BC30455: Argument not specified for parameter 'StrConn' of 'Public Sub New(StrConn As String, StrSp As String)'.
I think this has something to do with the fact that when you use code behind the class has to inherit 'page' and when it is created the inherited constructor is called instead of the paramater one.
Heres the class code:
Public Class spNoParam
Inherits Page
Private objConn As SqlConnection
Private cmdSp As SqlCommand
Private m_strConn As String
Private m_strSp As String
Sub New(ByVal StrConn As String, ByVal StrSp As String)
m_strConn = StrConn
m_strSp = StrSp
objConn = New SqlConnection(StrConn)
cmdSp = New SqlCommand(StrSp, objConn)
cmdSp.CommandType = CommandType.StoredProcedure
End Sub
Sub runSp()
objConn.Open()
cmdSp.ExecuteNonQuery()
objConn.Close()
End Sub
End Class
And this creates it:
'strConn and strSp are defined above
Dim myClass2 As New spNoParam(strConn, strSp)
Any ideas?
Thanks
Alex
ASP, SQL, VB6, Java Script and dubious guitar playing skills.
-
Dec 18th, 2002, 11:12 AM
#2
I'm not sure but why does it inherit from page?
-
Dec 18th, 2002, 07:12 PM
#3
Hyperactive Member
Since you're using the code behind feature the class that will support the page you're proggin must inherit from the Page Class. That being said, you'll never call the constructor for the class that supports the aspx page(ASP.NET will though), so you'll need to create a separate class that encapsulates the functionality you want to execute when the page is called. In the Page_Load event in the code behind, create a new instance of the separate class passing in your constructor arguments and then call whatever method you want or whatever.
-
Dec 18th, 2002, 07:30 PM
#4
I guess I was confused I thought that the class posted WAS the supporting class? That is why there are no page events used. Oh well I guess I've been wrong before and I'm sure this wont be the last time either.
Anyways, its like pvb suggested, all you have to do is remove the Inherits Page and then call this class from an actual aspx page.
-
Dec 19th, 2002, 11:58 AM
#5
Thread Starter
Addicted Member
For some reason adding a blank constructor as in something like:
public sub new
end sub
gets rid of the error and the class functions fine(?) Anyway have compiled this into a dll and imported it in code behind file.
Thanks for your help,
Alex
ASP, SQL, VB6, Java Script and dubious guitar playing skills.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|