Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
Hi,

I've been looking online for a while now and im sure there is going to be a very simple answer to this one. But I have a bit of code which is used in quite a few pages on our ASP.Net Application, and I would like to place it into a method/class so that I can use it throughout.

Here is the original code, would someone be able to help me put it into a class/method.

VB
Private Sub PopulateResults()
        'Populate the results dropdowns at the bottom of the page
        Dim SQLconn As SqlConnection
        Dim SQLComm As SqlCommand
        Dim SQLDr As SqlDataReader
        Dim GroupID As String = Session.Item("GroupID")

        SQLconn = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        SQLComm = New SqlCommand("prcHHGenResultList", SQLconn)
        SQLComm.CommandType = CommandType.StoredProcedure
        SQLComm.Parameters.AddWithValue("@GroupID", GroupID)

        Try
            SQLconn.Open()

            SQLDr = SQLComm.ExecuteReader()

            'Set the values and the text of the dropdown
            ddlSPBusinessPDVResult.DataSource = SQLDr
            ddlSPBusinessPDVResult.DataValueField = "ResultID"
            ddlSPBusinessPDVResult.DataTextField = "ResultDescription"
            ddlSPBusinessPDVResult.DataBind()

            SQLDr.Close()

            SQLComm = New SqlCommand("prcHHGenResultList", SQLconn)
            SQLComm.CommandType = CommandType.StoredProcedure
            SQLComm.Parameters.AddWithValue("@GroupID", GroupID)
            SQLComm.Parameters.AddWithValue("@ClientID", ClientID)

            'Add blank row first
            Dim lifirst As New ListItem("", "")
            ddlSPBusinessPDVResult.Items.Insert(0, lifirst)
        Catch ex As Exception
            Session("Error") = ex.Message
            Response.Redirect("Error.aspx")
        Finally
            SQLconn.Close()
        End Try
    End Sub


Any advice is much appreciated!!

Thanks,
Posted
Comments
JammoD87 27-Mar-12 7:28am    
Cheers guy's your solutions really helped me out!

I stumbled across this place from google and i'll certainly be coming back!!

Well...you already have it in a method...so....since you didn't really specify it's hard to know for sure what you're asking here.

I'll assume that this code is used in other places but for a different drop down? If that is the case, instead of using ddlSPBusinessPDVResult you'll want to pass in a drop down as a parameter. Pass it in ByRef instead of ByVal so that it will update it properly. If other things change in the different places you use the code, you'll want to pass those in as parameters too. For example, the values of your SQL parms will probably need to be passed in.

I'm thinking something like this:
VB
Public Sub FillDDL(ByRef ddl As System.Web.UI.WebControls.DropDownList, ByVal strGroupID As String, ByVal strClientID As String)
   'The code you have already, reworked using dll instead of dllSPBusinessPDVResult
   'And using the new strGroupID and strClientID parms
End Sub


Although, if you're calling this method on one web page, you'll probably also want to pass in the SQL command as a parm. That way you can set it up once and then re-use it everytime you call it instead of recreating it every time.
 
Share this answer
 
Comments
[no name] 26-Mar-12 12:12pm    
Passing a control to a business layer method adds a tight coupling the inhibits extensibility and testability of the code. A poor design IMO
Kschuler 26-Mar-12 12:19pm    
Good point. I guess I was just trying to get the basic concept out there.
Hello

You can just use the value of session instead of session

for example:

VB
Public Function PopulateResults(groupID As String ... and somethings else) As String
	'...

	Try
		'...

		'At the last line of Try:
		Return String.Empty
	Catch ex As Exception
		'Session["Error"] = ex.Message
		'Response.Redirect("Error.aspx")
		Return ex.Message
	Finally
		SQLconn.Close()
	End Try
End Function


Then instead of calling the method, use this code:
VB
Dim [error] As String = PopulateResults(groupID)

If Not String.IsNullOrEmpty([error]) Then
	Session("Error") = [error]
	Response.Redirect("Error.aspx")
End If
 
Share this answer
 
v3
Comments
[no name] 26-Mar-12 12:10pm    
A very poor implementation to return exception messages from methods.
Shahin Khorshidnia 26-Mar-12 12:45pm    
Sorry about editing your solution!!
Create the method in your business layer and return a datasource that can be used to bind the controls in your UI. Many people try to create similar methods and pass the control to be populated as a parameter but that doesn't allow for the loose coupling an application needs for flexibility and extensibility.

You should also learn about the using state
C#
using(SqlConnection conn = new SqlConnection(...))
{

}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900