[RESOLVED] Using Try Catch inside of WebMethod
I have a WebMethod that is inside of an ASP.NET Web Service application. I use a Try Catch block inside the WebMethod. If an exception is thrown and I want to see the ex.Message is there a way to use a messsagebox? Maybe there is some Imports statement that can be used so you can use a messagebox inside an ASP.NET Web Service application. I could declare a string for use to display the ex.Message but then the WebMethod would have to return that string somehow. That's something I don't know how to do. As long as no exceptions are thrown I'm ok but if an exception is thrown it's good to know what it is. The ASP.NET Web Service application is using .NET Framework 2.0.
In another thread gep13 was talking about creating a class so that you could return more than one thing from a WebMethod and if that's what I have to do that's fine but if there's a simpler way to see the ex.Message then that's ok too.
Re: Using Try Catch inside of WebMethod
Hello,
If you use a Try/Catch in the Web Service code and you actually catch an exception, then you have the option to throw that exception to the caller, in this case the Windows Form application. As a result, if you then also use a Try/Catch in the Windows Form application, then you will see the exception there.
However, this isn't the nicest of solutions. Typically, you see solutions where what you get back is an error code from a web service call. This could be included as a property of a class that you return from the web service. i.e. have a property called ErrorCode, where the default is 0 when things go as expected, however, when this isn't the case, you can return a "well-known" error code, which you can inspect on the client.
Another approach would be to rethrow the exception as a SoapException in which you can provide some more details. You can find more information about this here:
http://msdn.microsoft.com/en-us/libr....71).aspx#Y233
Hope that helps!
Gary
Re: Using Try Catch inside of WebMethod
In another thread you said :
Quote:
If you needed to return multiple "things" back from the WebMethod, then you could return a class of your creation. This class would then contain the properties that you want to return, and all you would need to do is return an instance of this class, loaded with the values that you require.
I'm not clear on how I would return a class using a WebMethod. I looked into creating classes already so I know something about that but it's not clear to me how I would return a class through a WebMethod. Would the WebMethod be a Function or a Sub or something else?
Re: Using Try Catch inside of WebMethod
Hello,
Ok, lets say we have a Person class (typical example) defined as:
Code:
Public Class Person
Private _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
Private _lastName As String
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal value As String)
_lastName = value
End Set
End Property
Private _age As Integer
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(firstName As String, lastName As String, age As Integer)
Me.FirstName = firstName
Me.LastName = lastName
Me.Age = age
End Sub
End Class
You could then have a couple web methods defined as:
Code:
<WebMethod()> _
Public Function GetSinglePerson() As Person
Dim entityX As New Person
entityX.FirstName = "Entity"
entityX.LastName = "X"
entityX.Age = 21
Return entityX
End Function
<WebMethod()> _
Public Function GetPeople() As List(Of Person)
Dim people As New List(Of Person)
people.Add(New Person("Entity", "X", 21))
people.Add(New Person("gep", "13", 21))
Return people
End Function
Then, in your calling application, you can access these by doing something like:
Code:
Imports WindowsApplication1.PeopleWebService
Public Class Form1
Dim webService As New Service1()
Private Sub GetPersonButton_Click(sender As System.Object, e As System.EventArgs) Handles GetPersonButton.Click
Dim singlePerson As Person = webService.GetSinglePerson()
MessageBox.Show(String.Format("FirstName: {0} LastName: {1} Age: {2}", singlePerson.FirstName, singlePerson.LastName, singlePerson.Age))
End Sub
Private Sub GetPeopleButton_Click(sender As System.Object, e As System.EventArgs) Handles GetPeopleButton.Click
Dim people As Person()
people = webService.GetPeople()
For Each singlePerson As Person In people
MessageBox.Show(String.Format("FirstName: {0} LastName: {1} Age: {2}", singlePerson.FirstName, singlePerson.LastName, singlePerson.Age))
Next
End Sub
End Class
Notice that the Person class is not defined within your Windows Form application, but rather, only in your Web Service Application. When you create the reference to your Web Service in your Windows Application the information about the "make-up" of the Person class is transferred over to the Windows Form application.
Hope that helps.
Let me know if there is anything you are not sure about.
Gary
Re: Using Try Catch inside of WebMethod
Very very helpful. Thanks again.
Re: [RESOLVED] Using Try Catch inside of WebMethod
Re: Using Try Catch inside of WebMethod
hello i have asimilar issue
currently i have this code which returns onky one value ie the las column last row value
i am not able to get multiple rows * columns
[WebMethod]
public string[] GetData(string param1 ,string param2 ,string param3 )
{
string MyConString = ";";
string sql = "Select * from table Where column1=@param1 and column2 =@ param2 and column3 =@ param3 ";
SqlDataAdapter da = new SqlDataAdapter(sql, MyConString);
da.SelectCommand.Parameters.Add("@param1", SqlDbType.VarChar, 50).Value =param1 + "";
da.SelectCommand.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = param2 + "";
da.SelectCommand.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = param3 + "";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["param1"].ToString(), i);
items.SetValue(dr[" param2 "].ToString(), i);
items.SetValue(dr[" param3 "].ToString(), i);
i += 1;
}
return items;
}
Re: [RESOLVED] Using Try Catch inside of WebMethod
Hello,
I don't think the code is doing what you "think" it does. You have a single dimensional array, and within the foreach loop you are setting the same index in the array each time.
Can you explain in words what you are trying to achieve, and we can work from there.
Gary