Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
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 26-Mar-12 5:57am
JammoD871.9K
Comments
JammoD87 at 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!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  
Comments
Mark Nischalke at 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 at 26-Mar-12 12:19pm
   
Good point. I guess I was just trying to get the basic concept out there.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello
 
You can just use the value of session instead of session
 
for example:
 
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:
Dim [error] As String = PopulateResults(groupID)
 
If Not String.IsNullOrEmpty([error]) Then
	Session("Error") = [error]
	Response.Redirect("Error.aspx")
End If
  Permalink  
v3
Comments
Mark Nischalke at 26-Mar-12 12:10pm
   
A very poor implementation to return exception messages from methods.
Shahin Khorshidnia at 26-Mar-12 12:45pm
   
Sorry about editing your solution!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
using(SqlConnection conn = new SqlConnection(...))
{
 
}
  Permalink  
v4

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 26 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100