Translate page specific code to generic reusable class page code

ivgoneriding

Member
Joined
Feb 4, 2011
Messages
10
Programming Experience
Beginner
Please forgive me, I am a complete newb.

I have the following code which runs as part of my onpage load in a ASP.NET project and populates a drop down list from a SQL DB:
Dim sConnectionString = ConfigurationManager.ConnectionStrings("Freshhoovesdb").ConnectionString
Dim cxConn As SqlConnection
Dim cxCommand As SqlCommand
Dim cxSqlDataReader As SqlDataReader
Dim sSQL As String
Dim sConn As String
sSQL = "SELECT * FROM PersonType"
sConn = sConnectionString
cxConn =
New SqlConnection(sConn)
cxConn.Open()
cxCommand =
New SqlCommand(sSQL, cxConn)
cxSqlDataReader = cxCommand.ExecuteReader()
ddlSelectPersonType.DataSource = cxSqlDataReader
ddlSelectPersonType.DataBind()
ddlSelectPersonType.Items.Insert(0,
New ListItem("(Select)"))
cxConn.Close()

I would like to be able to reuse this code on other pages within my project, so would like to be able to move it to my class page, but am having trouble because the ddlselectpersontype is not declared in the base code - how do I declare a dropdownlist from a page in the base code?

Thanks
 
Put all that in a Sub e.g
VB.NET:
Public MyClass
Public Shared Sub PopulateCombo(ByVal genericddl As DropDownList)
Dim sConnectionString As String = ConfigurationManager.ConnectionStrings("Freshhoovesdb").ConnectionString
Dim cxConn As SqlConnection
Dim cxCommand As SqlCommand
Dim cxSqlDataReader As SqlDataReader
Dim sSQL As String
Dim sConn As String
sSQL = "SELECT * FROM PersonType"
sConn = sConnectionString
cxConn = New SqlConnection(sConn)
cxConn.Open()
cxCommand = New SqlCommand(sSQL, cxConn)
cxSqlDataReader = cxCommand.ExecuteReader()
genericddl.DataSource = cxSqlDataReader
genericddl.DataBind()
genericddl.Items.Insert(0, New ListItem("(Select)"))
cxConn.Close()
End Sub
End Class

Now just call this method from any page you want:

MyClass.PopulateCombo(NameOfTheDropDownList)
 
Put all that in a Sub e.g
VB.NET:
Public MyClass
Public Shared Sub PopulateCombo(ByVal genericddl As DropDownList)
Dim sConnectionString As String = ConfigurationManager.ConnectionStrings("Freshhoovesdb").ConnectionString
Dim cxConn As SqlConnection
Dim cxCommand As SqlCommand
Dim cxSqlDataReader As SqlDataReader
Dim sSQL As String
Dim sConn As String
sSQL = "SELECT * FROM PersonType"
sConn = sConnectionString
cxConn = New SqlConnection(sConn)
cxConn.Open()
cxCommand = New SqlCommand(sSQL, cxConn)
cxSqlDataReader = cxCommand.ExecuteReader()
genericddl.DataSource = cxSqlDataReader
genericddl.DataBind()
genericddl.Items.Insert(0, New ListItem("(Select)"))
cxConn.Close()
End Sub
End Class
Now just call this method from any page you want:

MyClass.PopulateCombo(NameOfTheDropDownList)

Thanks, I'll give that a try.
 
I have created a sub on the base code page, as per the above instructions and have called the sub from my page load routine

VB.NET:
cxBaseCode.PopulateCombo(ssql, ddlselectaddress)

   Public Shared Sub PopulateCombo(ByVal ssql As String, ByVal ddlgeneric As DropDownList)
        Dim cxConn As SqlConnection
        Dim cxCommand As SqlCommand
        Dim cxSqlDataReader As SqlDataReader
        Dim sConn As String = sConnectionString

        cxConn = New SqlConnection(sConn)
        cxConn.Open()
        cxCommand = New SqlCommand(ssql, cxConn)
        cxSqlDataReader = cxCommand.ExecuteReader()
        ddlgeneric.DataSource = cxSqlDataReader
        ddlgeneric.DataBind()
        ddlgeneric.Items.Insert(0, New ListItem("(Select)"))
        cxConn.Close()


    End Sub
However, I now get this warning on my source code page on the line that calls the sub :
Access of shared member through an instance; qualifying expression will not be evaluated
How do I remedy this?
 
Last edited:
Back
Top