Results 1 to 5 of 5

Thread: class constructors with paramaters and code behind

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    UK
    Posts
    164

    Unhappy 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.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not sure but why does it inherit from page?

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    UK
    Posts
    164
    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
  •  



Click Here to Expand Forum to Full Width