Solved

Upgrading Prog. from VB6 to VB.net

Posted on 2004-04-05
11
629 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
Comment Utility
Are you trying to figure out how to put in the Try...Catch block code here?

Bob
0
 

Author Comment

by:sam5705
Comment Utility
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
Comment Utility
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
 

Author Comment

by:sam5705
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:sam5705
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now