Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...