Results 1 to 9 of 9

Thread: [RESOLVED] Returning error message from Web Service to Winforms

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Resolved [RESOLVED] Returning error message from Web Service to Winforms

    Hello, i'm new to web services. I'm trying to display the error message from the Web Service to the textbox in my Form1.


    Script on my button that calls the web service. (I have no issues on doing the edit thing, i have issues on retrieving the error message on my web service):

    If Not objWebServiceTest.editAccount(myObject, txtID.text) Then
    txtErrMsg.Text = objWebServiceTest.ReturnMessage
    Exit Sub
    End If



    Script on my web service (ASMX file):


    Public Function ReturnMessage() As String
    Return strMsg
    End Function



    <WebMethod()> _
    Public Function editAccount(ByVal myObject As Accounts, ByVal accountID As String) As Boolean
    Dim leditAccounts As Boolean = False
    Try
    leditNibs = "Assigning string to boolean to cause an error"
    *****Remove the scripts here******
    *****Remove the scripts here******
    *****Remove the scripts here******
    Catch ex As Exception
    strMsg = ex.Message
    End Try
    Return leditAccounts
    End Function


    *****************************************

    And when i query on the immediate window, i got this. I wanted to display that error message on my textbox in my winform.

    ?strMsg
    "Conversion from string "test lang" to type 'Boolean' is not valid."


    Once i click the button on my form, objWebServiceTest.ReturnMessage just returns a blank string. How do i get the value of the error message from my web service and display in on my textbox?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Returning error message from Web Service to Winforms

    Cases like this, what I've seen done is a response class of some kind is used... so the methods do not return simple types like boolean or string or integer... but will return a Reply object of some kind. That object then includes the status of the action, error messages, and any other information needed.

    So it might look something like this:
    Code:
    <WebMethod()> _
    Public Function editAccount(ByVal myObject As Accounts, ByVal accountID As String) As EditAccountReply
    Dim editReply as New EditAccountReply
    Try
    leditNibs = "Assigning string to boolean to cause an error"
    *****Remove the scripts here******
    *****Remove the scripts here******
    *****Remove the scripts here******
    editReply.Status = True
    Catch ex As Exception
    editReply.ErrorMessage = ex.Message
    editReply.Status = False
    End Try
    Return editReply
    End Function
    the EditAccountReply object can be as simple or as complex as you want it to be. If you were adding an account, you can then not only reply back with the success of the insert but you can then also return the ID:

    Code:
    <WebMethod()> _
    Public Function addAccount(ByVal myObject As Accounts, ByVal accountID As String) As AddAccountReply
    Dim addReply as New AddAccountReply
    Try
    leditNibs = "Assigning string to boolean to cause an error"
    *****Remove the scripts here******
    *****Remove the scripts here******
    *****Remove the scripts here******
    addReply.Status = True
    addReply.ID = -- what ever the id ends up being
    Catch ex As Exception
    addReply.ErrorMessage = ex.Message
    addReply.Status = False
    End Try
    Return addReply
    End Function
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Re: Returning error message from Web Service to Winforms

    Thanks for the reply tg. Actually, i have that code. The method i posted is the code on my ASMX file but i also have another code inside it that calls another object and process the real transaction. My problem here is that, how do i call/retrieve the error message from winform?

    My web service script:
    Code:
    Public Class Service
         Inherits System.Web.Services.WebService
        Dim strMsg As String = String.Empty
    
        <WebMethod()> _
        Public Function ReturnMessage() As String
            Return strMsg
        End Function
    
    
        <WebMethod()> _
        Public Function editAccount(ByVal strTest As String, ByVal accountID As String) As Boolean
            Dim leditAccounts As Boolean = False
            Try
                leditAccounts = "Assigning string to boolean to cause an error"
                'removed my scripts
                'removed my scripts
                'removed my scripts
            Catch ex As Exception
                strMsg = ex.Message
            End Try
            Return leditAccounts
        End Function
    End Class


    My winform script:
    Code:
    Imports ConsumeWebService.DevWebService
    Public Class frmMain
    
        Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
            Dim objWebService As New DevWebService.ServiceSoapClient
    
            Try
                If Not objWebService.editAccount("test", "123") Then
                    txtOutput.Text = objWebService.ReturnMessage
                    MsgBox(objWebService.ReturnMessage)
                Else
                    MsgBox("Account has been updated")
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            Finally
                objWebService = Nothing
            End Try
        End Sub
    End Class
    Upon clicking the confirm button, i only receive empty string. But if you debug that and check the error message at underlined portion, i can see the error message "Conversion from string "Assigning string to boolean to c" to type 'Boolean' is not valid."

    Code:
     <WebMethod()> _
        Public Function editAccount(ByVal strTest As String, ByVal accountID As String) As Boolean
            Dim leditAccounts As Boolean = False
            Try
                leditAccounts = "Assigning string to boolean to cause an error"
                'removed my scripts
                'removed my scripts
                'removed my scripts
            Catch ex As Exception
                strMsg = ex.Message
            End Try
            Return leditAccounts
        End Function
    Given those scripts of mine, how can i display that error message on my textbox?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Returning error message from Web Service to Winforms

    WebMethods are stateless... that's why I suggested returning a complex object. Data between multiple calls are not persisted. As soon as editAccount ends and the object is sent back to the client, it ceases to exist. So strMsg is not longer valid when you try to retrieve it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Re: Returning error message from Web Service to Winforms

    Thanks tg. I will try to do your suggestion. Thanks again.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Re: Returning error message from Web Service to Winforms

    I ended up changing the type of my function editAccount from Boolean to String. Then from the inner codes, passing the exception to a variable that eventually, will return by my function editAccount.

    It is working well now. Thanks

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Re: Returning error message from Web Service to Winforms

    Hi tg, i'm trying to solve my issue using your approach. I have some few questions regarding your method.

    Code:
    Dim objWS As New wsTest
    
    If Not objWS.editAccount(myObject, "123").Status Then
               MsgBox (objWS.editAccount(myObject, "123").ErrorMessage)
    End If
    That code is working, however i just wanted to know if that is the best way to do it? I mean, i already call the function on the If statement then i will call the same function again just to display the error message. Is there any other way to do it? Maybe calling the same function only once.

    Thanks

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Returning error message from Web Service to Winforms

    It's a function that returns an object, just like anything else...

    so... like this:
    Code:
    Dim objWS As New wsTest
    Dim myResult as _________ <- what ever the type is that it returns
    
    myResult = objWS.editAccount(myObject, "123")
    
    If Not myResult.Status Then
               MsgBox (myResult.ErrorMessage)
    End If
    Now it's only called once.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Re: Returning error message from Web Service to Winforms

    Hahaha. I read my question again and your answer and i find my question stupid. Lol. Anyway, its working now. thanks for the help tg.

    Edit:

    Dim myResult As New objWS.editAccount
    Last edited by hopia; Mar 2nd, 2016 at 10:45 PM.

Tags for this Thread

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