PetCar Posted February 4, 2004 Posted February 4, 2004 Hi, im quite new to XML, Soap and .NET. i got a question on how i can return a custom error when my database search didnt find anything. This is the code so far... <WebMethod()> Public Function WebSync_GetFileInfo(ByVal intFileID As Integer) As DataSet Dim strSQLConnectionString As String = "user id=ws;password=ws;initial catalog=misc;data source=127.0.0.1" Dim objSQLConnection As New SqlConnection(strSQLConnectionString) Dim strSQLQuery As String strSQLQuery = "SELECT * FROM files_files WHERE files_id = " & intFileID Dim mySQLCommand As SqlDataAdapter = New SqlDataAdapter(strSQLQuery, objSQLConnection) Dim dsFiles As New DataSet() mySQLCommand.Fill(dsFiles, "files_id") Dim MyTable As New DataTable() MyTable = dsFiles.Tables(0) If MyTable.Rows.Count = 0 Then 'Show some kind of error message Else Return dsFiles End If End Function What i want to do is to show a custom errormessage in XML when no files where found in the database. But i cant return a string when the function expects to receive a dataset. How can i show an error in xml instead of Nothing? Thanks! // Peter Quote
Administrators PlausiblyDamp Posted February 4, 2004 Administrators Posted February 4, 2004 You could throw an exception, this would be received at the client as a SoapException if I remember correctly Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PetCar Posted February 4, 2004 Author Posted February 4, 2004 How do i throw an exeption? All i get on client side is an error message. What i want to do is show something like: <xml> <errormessage>File ID not found</errormessage> </xml> // Peter Quote
Administrators PlausiblyDamp Posted February 4, 2004 Administrators Posted February 4, 2004 in the web service Public Function Test(ByVal i As Integer) As DataSet If i = 1 Then Dim ds As New DataSet 'do whatever with dataset here Return ds Else Throw New ApplicationException("Some error or other occured") Return Nothing End If End Function in the client you can handle the error like Dim x As New localhost.Service1 Dim ds As DataSet Try ds = x.Test(1) 'should return data ds = x.Test(2) Catch ex As System.Web.Services.Protocols.SoapException 'error occured MessageBox.Show(ex.Message) End Try have a look Here for a bit more info on SoapException Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PetCar Posted February 6, 2004 Author Posted February 6, 2004 Thanks alot! That did help me! :) // Peter Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.