Thread: Survey Engine
View Single Post
  #2 (permalink)  
Old March 2nd, 2008, 04:41 PM
dparsons dparsons is offline
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmmm. Imar may disagree with me and there may be a good reason why he did this, but here is my take on it. IMHO the function that you provided in your post could be further seperated out since there is a coupling of Business Logic along with DataAccess in this function. What I might do is something like this:

Public Class fooBLL
Public Sub SaveResponses()
     Dim QuestionIDs As Collection = GetQuestionIDsForSurvey(mform.Request.QueryString( "surveyID"))
     If Not fooDAL.SaveResponses(QuestionIDs) Then 'DoSomething
End Sub
End Class

Public Class fooDAL
Public Shared Function SaveResponses(ByVal QuestionIDs As Collection) as Boolean
         For i As Integer = 0 To QuestionIDs.Count - 1

                'ensure the user selected one of the option buttons for this specific question ID
                If mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()) <> "" Then

                    'Create a command object
                    Dim mCommand As SqlCommand = New SqlCommand("sprocResponseInsertItem", mConnection)

                    'set it to the type of 'stored procedure'
                    mCommand.CommandType = CommandType.StoredProcedure

                    'add in two parameters: the question ID and the choice of the user (A,B,C,or D)
                    mCommand.Parameters.AddWithValue("@questionID", QuestionIDs(i + 1).ToString())
                    mCommand.Parameters.AddWithValue("@selection", mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()))

                    'open the connection and execute the stored procedure
                    mConnection.Open()
                    mCommand.ExecuteNonQuery()

                    'close the connection and dispose of the command
                    mConnection.Close()
                    mCommand.Dispose()

                End If
            Next

            Return True

End Function
End Class

The only thing that i did not demonstrate abouve that you would need to do is create an additional array to pass into your DAL method to replace this line:
If mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()) <> "" Then

Once that is complete, if you were to port this to a windows app you simply would have to change the mechanisim by which you return the QuestionIDs and pass them off for processing in the DAL.

hth.

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========