Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 2nd, 2008, 03:07 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Survey Engine

Hello. In chapter 4 Survey Engine I found this code. Procedure SaveResponses() writes user responses to the database. I can't understand how does it correlate with N-tier design where Data Access Level is completely independent from Application Level. What I mean.
Code:
Dim mform As HttpContext = HttpContext.Current
Dim QuestionIDs As Collection = GetQuestionIDsForSurvey(mform.Request.QueryString("surveyID"))
How this code is supposed to work if we move from website to Windows Application for example?

Code:
    
   Public Shared Function SaveResponses() As Boolean

        Using mConnection As New SqlConnection(Config.ConnectionString)

            ' get the request context...(we can access the values from the form 
            'that the user last posted from here!)
            Dim mform As HttpContext = HttpContext.Current

            'get the list of questions from the db
            Dim QuestionIDs As Collection = GetQuestionIDsForSurvey(mform.Request.QueryString("surveyID"))

            'iterate through each of the question ID's and add the appropriate response
            'to the responses table
            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 Using


    End Function


  #2 (permalink)  
Old March 2nd, 2008, 04:41 PM
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
================================================== =========
  #3 (permalink)  
Old March 3rd, 2008, 02:30 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes. Of course what you posted is better. And to be honest I can't believe Imar wrote Chapters 4 and 7 (I haven't read the remainder yet). Take this function from Capter 7 - Photogallery
Code:
    Public Shared Function InsertCollection(ByVal pc As PhotoCollection) As Boolean

        Try

            Dim conn As New SqlConnection()
            Dim cmd As New SqlCommand()
            conn.ConnectionString = PhotoDB.ConnectionString
            cmd.Connection = conn
            conn.Open()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "add_collection"

            ' Create a SqlParameter for each parameter in the stored procedure.
            Dim nameParam As New SqlParameter("@name", pc.name)
            Dim descParam As New SqlParameter("@desc", pc.description)
            cmd.Parameters.Add(nameParam)
            cmd.Parameters.Add(descParam)

            cmd.ExecuteNonQuery()

            Return True

        Catch ex As Exception


            Throw (ex)

        End Try

    End Function
Where connection is closed?
Code:
Catch ex As Exception


      Throw (ex)

End Try
What is this?

  #4 (permalink)  
Old March 3rd, 2008, 04:13 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Correct, I didn't write those chapters.

AFAICS, the connection is not closed.

The Throw (ex) is not the most optimal solution. I think the idea was this:

Try
  ' Some risky code
Catch
  LogError()
  Throw
End Try

This way, the LogError() method is called to log the error somehow. The exception is the rethrown to cause it to bubble up to the ASP.NET error handling.

If you don't have error logging like this, you can remove the Try Catch statement altogether and just rely on ASP.NET error handling.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #5 (permalink)  
Old March 3rd, 2008, 05:09 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, thank you.
Imar, I really like your blog's article "Building Layered Web Applications". And I like like this book's approach because it gives an excellent almost real-life experience to the reader. (I haven't seen such a book which would describe several small real-life but not very complex projects). And I just think that you (as a very smart author for me) should supervise your co-authors :) Because readers can gain a wrong impression of this book.

PS I'm looking forward to new editions in ASP.NET 3.5 :)

  #6 (permalink)  
Old March 3rd, 2008, 09:08 AM
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

Appologies Imar, I didn't realize that there were co-authors on this book.


Dennis: Beginning ASP.NET 3.5 is available (depending on your locale of course) http://www.amazon.com/Beginning-ASP-...4549651&sr=8-2

hth.

================================================== =========
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
================================================== =========
  #7 (permalink)  
Old March 3rd, 2008, 02:34 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Dennis,

There are currently no plans for a 3.5 version of the book. However, as Doug pointed out, I just released Beginning ASP.NET 3.5 with C# and VB.NET. You may also want to take a look at Professional ASP.NET 3.5, also published by Wrox.

Doug: you're right. The other two guys on the front cover of the book are just my other schizophrenic personalities.... ;) Don't tell anyone....

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Survey Engine - Does not work mpremji BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 9 June 4th, 2007 12:35 PM
Survey Engine anitafn2005 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 February 18th, 2007 01:56 AM
Survey engine anitafn2005 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 13 February 11th, 2007 04:49 PM
Survey engine anitafn2005 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 October 8th, 2006 04:17 PM
Wrox Survey Engine Berggreen BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 7 June 25th, 2006 05:21 PM



All times are GMT -4. The time now is 11:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.