• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

Upgrading Prog. from VB6 to VB.net


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")
            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

        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

        Err.Raise(Err.Number, Err.Source, Err.Description)
      End Function
  • 5
  • 4
3 Solutions
Bob LearnedCommented:
Are you trying to figure out how to put in the Try...Catch block code here?

sam5705Author Commented:
It's not that so much, but trying to trap the same error messages using the catch part
Bob LearnedCommented:
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.

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

sam5705Author Commented:

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
Bob LearnedCommented:
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.

sam5705Author Commented:
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
Bob LearnedCommented:
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.

sam5705Author Commented:
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.
Bob LearnedCommented:
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.

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

 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.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now