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

Upgrading Prog. from VB6 to VB.net

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
sam5705
Asked:
sam5705
  • 5
  • 4
3 Solutions
 
Bob LearnedCommented:
Are you trying to figure out how to put in the Try...Catch block code here?

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

Bob
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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

Bob
0
 
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
0
 
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.

Bob
0
 
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.
Thanks
0
 
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.

Bob
0
 
farsightCommented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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