Solved

Upgrading Prog. from VB6 to VB.net

Posted on 2004-04-05
11
631 Views
Last Modified: 2007-12-19
Hi,

I am upgrading some code from VB6 to vb.net , cant seem to figure out how to get it to do the same as it did previosly using the try catch and finally, and with the same errors as the exception.

Public Function Send(ByVal objMessage As CMessage) As CResponse

On Error GoTo Handler
            
      ' Declare HTTP object, so that we can send the XML over HTTP to the server
        Dim objHTTP As MSXML2.XMLHTTP30
        objHTTP = New MSXML2.XMLHTTP30

   

        Dim objResponse As CResponse
        objResponse = New CResponse


        ' Initialise the HTTP request to use the POST method and be Async
        objHTTP.open("POST", mstrURL, False, mstrUsername, mstrPassword)
        Dim strXYZ As String
        'objHTTP.Send strXYZ
        If (objMessage.TransmissionFormat = CMessage.MessageTransmissionFormat.FORMAT_HTML) Then
            objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        Else
            objHTTP.setRequestHeader("Content-Type", "text/xml")
        End If

        objHTTP.send(objMessage.GetMessageForTransmission) ' send our XML doc to the gateway

        ' Assign the status code, status text and the return data to the function
        objResponse.HTTPStatus = CStr(objHTTP.status)
        objResponse.HTTPStatusText = objHTTP.statusText

        objResponse.ResponseText(objHTTP.responseText) = objMessage.TransmissionFormat
        'objResponse.XML = objHTTP.responseXML



        Send = objResponse

        ' Set scope of objects to nothing
        'UPGRADE_NOTE: Object objHTTP may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1029"'
        objHTTP = Nothing
        Exit Function

Handler:
        Dim lErrNumber As Integer
        Dim strErrSource As String
        Dim strErrDesc As String

        lErrNumber = Err.Number
        strErrDesc = Err.Description
        strErrSource = Err.Source

        On Error GoTo Handler2

        objResponse.State = CSubmissionProcessor.SubmissionState.SS_ERROR


        Select Case lErrNumber
            Case -2146697211 ' This denotes the message could not be sent
                objResponse.StateText = "The message could not be sent.  This may mean that the Gateway server is currently offline or that the URL is incorrect."
                Send = objResponse

            Case -2146697208 ' internal MSXML3 Automation error
                ' This error sometimes occurs when the server certificate cannot be trusted
                Send = UntrustedCertificateResend(objMessage)

            Case Else
                objResponse.StateText = lErrNumber & ": " & strErrDesc
                Err.Raise(lErrNumber, strErrSource, strErrDesc)
        End Select
        Exit Function

Handler2:
        Err.Raise(Err.Number, Err.Source, Err.Description)
            
      End Function
0
Comment
Question by:sam5705
  • 5
  • 4
11 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 125 total points
ID: 10759535
Are you trying to figure out how to put in the Try...Catch block code here?

Bob
0
 

Author Comment

by:sam5705
ID: 10761816
It's not that so much, but trying to trap the same error messages using the catch part
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 10765138
This looks like you are still using a COM library for XML/HTTP support.  Did you want to stick with this, or change to ASP.NET?  If you go with the COM XML library, then you won't be able to trap the error messages using any kind of special exceptions.

Bob
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:sam5705
ID: 10767464
Hi,

what I'm trying to do is create a windows service using vb.net. That parts not to bad. I guess what I am really asking , is by upgrading the code from VB6 to Vb.Net can I still catch the same error messages. Ie under the catch part which type of exception do I catch. Has MSXML got error messages that  I can capture in the more structured way.Or if I capture all exceptions how do then ascertain which error number actually happened - I guess I'm a bit confused about exceptions.
Many Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10767801
First of all, in .NET you have special exceptions:  System.Data.SqlClient.SqlException, System.IO.FileNotFoundException, System.ArithmeticException, System.DivideByZeroException, etc..  You will notice, if you look in the help file, that these classes all have the same base (System.Exception).  What each one does is take the standard exception as a base, and build on it special information about exceptions.  These classes are part of the .NET framework.  

MSXML is a COM library, and which doesn't fit into the exception handling that .NET has.  Unfortunately, the type of handling that you want may not be possible in the structured Try...Catch...End Try paradigm.  The Exception class does not support error numbers AFAIK.  

That is why I asked if you could go away from migrating this code, and actually starting with the XML/HTTP support that .NET has.  Then you could try to think about the structure exception handling.

Bob
0
 

Author Comment

by:sam5705
ID: 10767970
Thats great, I think thats why I was getting myself in  a bit of a twist!
If I did change to the .net paradigm what classes would I use to do the same thing, from a vb.net service environment?
Many Thanks
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 125 total points
ID: 10768022
I am just learning all of this with HTTP/XML.  I have a good handle on exceptions.  If you will be patient with me, I will try to give you as much information as I know.

Bob
0
 

Author Comment

by:sam5705
ID: 10768343
Thats great, I'll try and look some stuff up as well, although a lot of it seems targetted from an ASP .net view point, and less from a windows service point.
Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10768395
I think what you will need will be the System.Web.HttpRequest class, with HttpRequest.HttpMethod = "POST".  Even though it is in the System.Web namespace, you can still use outside of the ASP.NET realm.

Bob
0
 
LVL 12

Expert Comment

by:farsight
ID: 10768429
I, too, recommend using .NET's support for retrieving HTML.

But, if you stick with those COM objects, you could probably use GetLastWin32Error() to get the error information you need for your catch block.  Search for some online articles on that for more details.  (I have no direct experience with this, just the theory from reading.)

--- quoted from the VS.NET 2003 object browser ---

Public Shared Function GetLastWin32Error() As Integer
     Member of: System.Runtime.InteropServices.Marshal

Summary:
 Returns the error code returned by the last unmanaged function called using platform invoke that has the System.Runtime.InteropServices.DllImportAttribute.SetLastError flag set.  

Return Values:
The last error code set by a call to the Win32 SetLastError API method.  
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question