Results 1 to 30 of 30

Thread: Response.Write is not a good thing to use on ASP.NET pages.

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Response.Write is not a good thing to use on ASP.NET pages.

    http://www.vbforums.com/archive/index.php/t-395604.html
    Why not?

    When I hit an exception in my code behind, can I put up a windows-style message box? I'd like to pass the variable ex.message to it, but none of these is working:
    Code:
                ' Response.Write("<script language='javascript'>alert(ex.message);</script>")
                ' Response.Write("<script language='javascript'>alert(" + ex.Message + ");</script>")
                ' Response.Write("<script language=javascript>alert('This is a dynamic alert.');</script>")

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Funnily enough, this was once discussed in Chit Chat of all places. I will paste what I said here (with some amendments to make me appear smarter than I am)

    Using Response.Write is bad. Using Response.Write to write javascript is worse. Using Response.Write to write "cool" AJAX is shameful, something I'd never admit to in public.

    The reason is, Response.Write occurs when the response stream is being sent and so the output can be sent at any point in the page rather than when it should be sent. Additionally, if there is a problem with the Response.Write, you could actually mess up the stream, end it, or just mangle the page. If you create a script block of a function you've made, it's always best to use Page.RegisterClientScript (or its equivalent) so that the script is output to the correct place in the page. Incorrect placement of scripts (hence, Response.Write) can cause problems especially when you're working with the DOM. ASP.NET when used correctly effectively negates the purpose of Response.Write. In fact, Response.Write is very much like <% inline ASP.NET code %>; quick and dirty and good for tutorials.


    For your specific case, you can use

    Code:
    Page.RegisterStartupScript("mymsg","<script type='text/JavaScript'>alert('" & ex.Message & "');")

  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    Funnily enough, this was once discussed in Chit Chat of all places. I will paste what I said here ...
    Yes, I thought it amusing that even just googling stuff led me to one of your brilliant explanations!

    Thanks.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    You actually found that thread via google?

    What keywords did you use?

  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Can't remember but I'll see if I can recreate it tomorrow, have to leave and pick up children...

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    It would be easier to just search vbforums first.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    .


    For your specific case, you can use

    Code:
    Page.RegisterStartupScript("mymsg","<script type='text/JavaScript'>alert('" & ex.Message & "');")
    My compiler says that is obsolete (probably asp.net 1.1 to 2.0 difference). So this is what I tried:
    Code:
            Try
                adapter.Fill(dsInvoices)
            Catch ex As Exception
                ' Response.Write("<script language='javascript'>alert(ex.message);</script>")
                ' Page.RegisterStartupScript("mymsg", "<script type='text/JavaScript'>alert('" & ex.Message & "');")
                Dim t As Type = Me.[GetType]()
                If Not ClientScript.IsClientScriptBlockRegistered(t, "ExceptionMessage") Then
                    ClientScript.RegisterClientScriptBlock(t, "ExceptionMessage", "<script type='text/JavaScript'>alert('" & ex.Message & "');")
                End If
                conn.Close()
                Exit Sub
            End Try
    This is probably a stupid question, but when would I actually see the alert? I forced the exception by trying to execute a non-existent sp, and all I saw was a blank IE window.

    Thanks for your help.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Yes, in ASP.NET 2.0+, it's ClientScript instad of Page.Register....

    Note that ClientScript.RegisterClientScriptBlock != Page.RegisterStartupScript. The equivalent is ClientScript.RegisterStartupScript.

    RegisterStartupScript means "execute this as soon as the page loads", while RegisterClientScriptBlock means "here's some new JavaScript. Put it on the page, I might use it later."


    Also, move your conn.Close() to the Finally block, not the Catch.


    Also also, you need to close your script tag.

    "<script type='text/JavaScript'>alert('" & ex.Message & "');</script>"

  9. #9

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Nag, nag, nag... (just kidding).

    There seems to be a missing end paren somewhere - I get error expected ')' (though I sure don't see it). But you're right that it's not right the way I had it. I just displayed a blank screen again.

    I have conn.Close where it is because I bail out next instruction. If I have Finally, how do I know if I'm there and things are good or bad? I usually only have Finally's at the end of my sub, not in the middle...("bad programmer!" you are probably going to say...)

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Try it simpler first.

    Code:
            Try
                adapter.Fill(dsInvoices)
            Catch ex As Exception
               
                    ClientScript.RegisterClientScriptBlock(Me.GetType, "ExceptionMessage", "<script type='text/JavaScript'>alert('Hello World!');</script>")
          
                Exit Sub
            Finally
                conn.Close()
            End Try

    The finally gets executed regardless of whether there is an exception or not. Since you logically would want to close the connection after the operation, the close should go into finally. Even if it's in a loop of some sort, it'll still close it properly.

    <NagMode>
    Also, if the dataadapter is using the conn, you don't need to open it and therefore you don't need to close it for that matter.
    </NagMode>

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    Try it simpler first.

    Code:
            Try
                adapter.Fill(dsInvoices)
            Catch ex As Exception
               
                    ClientScript.RegisterClientScriptBlock(Me.GetType, "ExceptionMessage", "<script type='text/JavaScript'>alert('Hello World!');</script>")
          
                Exit Sub
            Finally
                conn.Close()
            End Try
    Okay, this "simpler" way worked. I don't know what I was doing wrong before. I have a question. I replaced 'hello world' with 'Program Error in RowDataBound event of grid'. Can I concatenate ex.message to this? (Assuming the answer is yes, I guess I should ask how?) Also, what is "ExceptionMessage"?

    Now to get *really* picky, the reason I resurrected this thread is because in my other thread More Advanced Gridview Updating I was having an error processing my even-numbered rows. I took me a while to find because I didn't have a proper exception handler. So now that I do, how would I bail out of a row edit in the midst of a RowDataBound event? I'd like to put the entire grid back in view mode because I am getting an exception, there is no current data displayed for the row, yet the user is in edit mode and could really mess things up (not that this bug would ever make its way into production... )

    Thanks.

  12. #12

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by MMock
    Okay, this "simpler" way worked. I don't know what I was doing wrong before. I have a question. I replaced 'hello world' with 'Program Error in RowDataBound event of grid'. Can I concatenate ex.message to this? (Assuming the answer is yes, I guess I should ask how?) Also, what is "ExceptionMessage"?
    This is bad. Now I'm quoting myself (much worse than talking to oneself!)

    ExceptionMessage is the name of the script block, correct? So if I do more than one, I would give each block a unique name? Can you explain this in the bigger picture of things (like the whole program, not just a procedure) so I understand it and can implement it correctly?

    And I've figured out the concatenation question.

  13. #13

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    I have an exception handler in my RowDataBound event and my RowUpdating event. I am hitting them both. The former works but the latter is saying "Done, but with errors on page" and the details say Expected ')'. I double-checked and ensured it is the same code both places. Here are the two blocks.

    This is in RowDataBound. I see this message.
    Code:
                    Catch ex As Exception
                        Dim t As Type = Me.[GetType]()
                        If Not ClientScript.IsClientScriptBlockRegistered(t, "ExceptionMessage") Then
                            ClientScript.RegisterClientScriptBlock( _
                                t, _
                                "ExceptionMessage", _
                                "<script type='text/JavaScript'>alert('Exception in RowDataBound event of grid: " & ex.Message & "')</script>")
                            End If
                        Exit Sub
                    End Try
    This is in RowUpdating. I don't see the message and I get the warning icon:
    Code:
                Catch ex As Exception
                    Dim t As Type = Me.[GetType]()
                    If Not ClientScript.IsClientScriptBlockRegistered(t, "ExceptionMessage2") Then
                        ClientScript.RegisterClientScriptBlock( _
                            t, _
                            "ExceptionMessage2", _
                            "<script type='text/JavaScript'>alert('Exception in RowDataBound event of grid: " & ex.Message & "')</script>")
                        End If
    
                End Try
    Do you see what is wrong?

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by MMock
    Okay, this "simpler" way worked. I don't know what I was doing wrong before. I have a question. I replaced 'hello world' with 'Program Error in RowDataBound event of grid'. Can I concatenate ex.message to this? (Assuming the answer is yes, I guess I should ask how?) Also, what is "ExceptionMessage"?

    Now to get *really* picky, the reason I resurrected this thread is because in my other thread More Advanced Gridview Updating I was having an error processing my even-numbered rows. I took me a while to find because I didn't have a proper exception handler. So now that I do, how would I bail out of a row edit in the midst of a RowDataBound event? I'd like to put the entire grid back in view mode because I am getting an exception, there is no current data displayed for the row, yet the user is in edit mode and could really mess things up (not that this bug would ever make its way into production... )

    Thanks.
    The even rows thing sounds like you're missing out on a check for Row and Alternate Row. Do you have that check in your If statement in the handler event? You can check the rowtype to see if it's a normal row or alternate row, or header and so on.

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by MMock
    This is bad. Now I'm quoting myself (much worse than talking to oneself!)

    ExceptionMessage is the name of the script block, correct? So if I do more than one, I would give each block a unique name? Can you explain this in the bigger picture of things (like the whole program, not just a procedure) so I understand it and can implement it correctly?

    And I've figured out the concatenation question.
    Yes, each script 'block' would get a unique name. This is used for when you want to check in 'some other' postback whether a script block of that name has already been registered or not.

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by MMock
    I have an exception handler in my RowDataBound event and my RowUpdating event. I am hitting them both. The former works but the latter is saying "Done, but with errors on page" and the details say Expected ')'. I double-checked and ensured it is the same code both places. Here are the two blocks.

    This is in RowDataBound. I see this message.
    Code:
                    Catch ex As Exception
                        Dim t As Type = Me.[GetType]()
                        If Not ClientScript.IsClientScriptBlockRegistered(t, "ExceptionMessage") Then
                            ClientScript.RegisterClientScriptBlock( _
                                t, _
                                "ExceptionMessage", _
                                "<script type='text/JavaScript'>alert('Exception in RowDataBound event of grid: " & ex.Message & "')</script>")
                            End If
                        Exit Sub
                    End Try
    This is in RowUpdating. I don't see the message and I get the warning icon:
    Code:
                Catch ex As Exception
                    Dim t As Type = Me.[GetType]()
                    If Not ClientScript.IsClientScriptBlockRegistered(t, "ExceptionMessage2") Then
                        ClientScript.RegisterClientScriptBlock( _
                            t, _
                            "ExceptionMessage2", _
                            "<script type='text/JavaScript'>alert('Exception in RowDataBound event of grid: " & ex.Message & "')</script>")
                        End If
    
                End Try
    Do you see what is wrong?
    Do a view source on the page, look at the javascript created. You may be able to see the problem there. Paste it here if you can't find it.

  17. #17

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    The even rows thing sounds like you're missing out on a check for Row and Alternate Row. Do you have that check in your If statement in the handler event? You can check the rowtype to see if it's a normal row or alternate row, or header and so on.
    Yes that was it, quit cross-threading! And maybe it's a moot point because really bad bugs like that one are usually caught in test, but my question was once I am correctly catching an exception...how do I handle it? But skip it, it was too picky.

  18. #18

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    Do a view source on the page, look at the javascript created. You may be able to see the problem there. Paste it here if you can't find it.
    Once again, this is taking a back burner (until Monday).Today I want to finish a page so my customer can start testing and tomorrow I have a day off - one daughter is reader of the month and the other is performing african dance at school.

    Thanks.

  19. #19
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by MMock
    Yes that was it, quit cross-threading! And maybe it's a moot point because really bad bugs like that one are usually caught in test, but my question was once I am correctly catching an exception...how do I handle it? But skip it, it was too picky.
    Depends on your needs. You can throw the exception (don't handle it) and let the user see it. But that confuses users. What I see most often is that the exception is caught, logged, and an error message shown to the user. "An error has occurred. Please contact your administrator. Look at the event logs. Nag nag nag."

    Then when they report it to you, have a look at the log to see what the error was. It does inconvenience the user but is a better alternative to that YSOD.

  20. #20

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Maybe this week I will actually understand enough to be able to close this thread...

    Here is my question now. As you might remember, I coded a lot of my pages with response.write in my exception handler with no intention of leaving them that way when they went into production. So now I am doing my finishing touches on them. I have an exception in a class file I am calling. So what is the best way to code the exception handling? I can't put RegisterClientScriptBlock because there's no such thing as ClientScript (since it's a vb module, not vb code-behind, I am assuming). Do I code my class file function to pass back something to indicate "an exception occured" and also pass back ex.message, or do I ignore exception handling in the class file and let the exeption get bubbled up to its caller? Or none of the above???

    Thanks.

  21. #21

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    I chose none of the above and went this way:

    Code:
            Try
    
                conn.Open()
                command.ExecuteNonQuery()
                ' int count = Int32.Parse(cmd.Parameters["@LastNameCount"].Value.ToString());
                iCount = CInt(command.Parameters("@mktarea_cnt").Value)
                GetMarketAreaCnt = iCount
    
            Catch ex As Exception
                ' Can't do reponse.write() here
                Dim s As String
                s = ex.Message
                Throw New Exception(ex.Message)
            Finally
                conn.Close()
                conn.Dispose()
                conn = Nothing
            End Try
    Good?

  22. #22

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    Yes, each script 'block' would get a unique name. This is used for when you want to check in 'some other' postback whether a script block of that name has already been registered or not.
    And if it has already been registered, I have to unregister it to use it again? Is something like this what I need to do application-wide:
    Code:
    ClientScript.RegisterClientScriptBlock(Me.GetType, "ExcPageAFunction1", "<script type='text/JavaScript'>alert('Exception');</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType, "ExcPageAFunction2", "<script type='text/JavaScript'>alert('Exception');</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType, "ExcPageAFunction3", "<script type='text/JavaScript'>alert('Exception');</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType, "ExcPageBFunction1", "<script type='text/JavaScript'>alert('Exception');</script>")
    etc., with all unique names?

  23. #23
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    The uniqueness is on a page level. You don't need to make it unique site-wide. If you have only one RegisterClientScriptBlock() on your page, then you can name it whatever you want.

  24. #24
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by MMock
    I chose none of the above and went this way:

    Code:
            Try
    
                conn.Open()
                command.ExecuteNonQuery()
                ' int count = Int32.Parse(cmd.Parameters["@LastNameCount"].Value.ToString());
                iCount = CInt(command.Parameters("@mktarea_cnt").Value)
                GetMarketAreaCnt = iCount
    
            Catch ex As Exception
                ' Can't do reponse.write() here
                Dim s As String
                s = ex.Message
                Throw New Exception(ex.Message)
            Finally
                conn.Close()
                conn.Dispose()
                conn = Nothing
            End Try
    Good?
    You're catching an exception and throwing an exception - that's equivalent to not handling it. What is the purpose of doing it this way?

  25. #25
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Also, you declare a string s, give it ex.Message, but then don't use it anywhere...

  26. #26

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    Also, you declare a string s, give it ex.Message, but then don't use it anywhere...
    Sorry...at the time, that was just to have somewhere to put a bp and look at ex.message. It was never meant to stay, but thanks for pointing it out in case I needed a little jarring to see it for myself. I think it's gone already.

  27. #27

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Quote Originally Posted by mendhak
    You're catching an exception and throwing an exception - that's equivalent to not handling it. What is the purpose of doing it this way?
    Is it? I just tend to cringe if I don't see any exception handling, so I wanted to make it obvious that I *want* to not handle it and bubble it up to my caller, not that I just forgot to handle it. I wasn't sure about that. So you're saying just remove it? Maybe with a comment that I'm letting the caller handle it?

  28. #28
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    To make it obvious that you're not handling it, don't handle it.

    When you do what you did, the operation becomes expensive because, first, the compiler stops execution immediately and 'catches' the exception. Then when you throw the exception, that's another code break.

    Using try_catch is not expensive, it's catching and throwing errors that's expensive.

    I'm not sure who the 'caller' is in this case but if you want your caller to handle it, then let it bubble up on its own. It is logical.

  29. #29

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    Okay, that makes sense.

    But what happens to things you would do in a Finally block if you are forsaking your exception handling? E.g...
    Code:
                Try
                    adapter.Fill(ds)
                Catch ex As Exception
                    Throw New Exception(ex.Message)
                    Exit Function
                Finally
                    conn.Close()
                End Try
    My conn will stay open?

  30. #30
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Response.Write is not a good thing to use on ASP.NET pages.

    This is where things like Using comes in handy.

    If you're using (ha) .net 2.0 onwards, then you can use using.

    Code:
    Using conn
    'conn.open()
    'something
    conn.close()
    End Using
    But you have to keep in mind, because a catch/throw is expensive it doesn't mean you get rid of it and just not use it. The rule of thumb is to use exception handling around code which does have a possibility of failure. So in your case, it's fine to leave it there.

    To address the specific example there, here's how I'd do it

    vb Code:
    1. Try
    2.                 adapter.Fill(ds)
    3.             Catch ex As Exception
    4.                 'Write to log file maybe
    5.             Finally
    6.                 conn.Close()
    7.             End Try
    8.  
    9. If ds Is Not Null Then
    10. 'Rest of Code
    11.  
    12.  
    13. End If
    14.  
    15. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width