# .NET and More > WPF, WCF, WF >  [RESOLVED] Returning error message from Web Service to Winforms

## hopia

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?

----------


## techgnome

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:


```
<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:



```
<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

----------


## hopia

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:*


```
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:*


```
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."



```
 <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?

----------


## techgnome

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

----------


## hopia

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

----------


## hopia

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

----------


## hopia

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



```
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

----------


## techgnome

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

so... like this:


```
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

----------


## hopia

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.  :Thumb:  

Edit:

Dim myResult As New objWS.editAccount

----------

