Share via


Deleting an Entity Using REST (VB)

[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]

As described in Deleting an Entity Using REST, the following Visual Basic code deletes a specified entity from a container.

Note

Credentials are required to access the service. To make a working sample, update following code and provide your existing authority id, container id and entity id you wish to delete.

If you are using the Microsoft Visual Studio integrated development environment, create a console application and add the following code.

Imports System
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Xml.Linq

Namespace DeleteEntityUsingREST

    Class Program

        ' Provide your data here
        Private userName As String
        Private password As String
        Const authorityId As String = "trworth-VB"
        Const containerId As String = "c3_VB"
        Const entityId As String = "e1"

        Const HttpDeleteMethod As String = "DELETE"
        Const ssdsContentType As String = "application/x-ssds+xml"

        Public Sub testMain()

            Console.Write("Username: ")
            userName = Console.ReadLine()
            Console.Write("Password: ")
            password = ReadPassword()

            Dim BookUri As String = String.Format("https://{0}.data.database.windows.net/v1/{1}/{2}", authorityId, containerId, entityId)
            Try

                Dim request As WebRequest = HttpWebRequest.Create(BookUri)
                request.Credentials = New NetworkCredential(userName, password)
                request.Method = HttpDeleteMethod

                ' Get the response and read it in to a string.
                Using response As HttpWebResponse = request.GetResponse()

                    If response.StatusCode <> HttpStatusCode.OK Then

                        Console.WriteLine("failed to delete the entityresourece.")
                    End If
                End Using

            Catch ex As WebException
                Dim errorMsg As String = ex.Message
                Console.WriteLine("Deletion Failed: {0}", errorMsg)
                If Not (ex.Response Is Nothing) Then
                    Using response As HttpWebResponse = ex.Response

                        Console.WriteLine("Unexpected status code returned: " + response.StatusCode.ToString())
                    End Using
                End If
            End Try
        End Sub 'testMain

        Public Function ReadResponse(ByVal response As HttpWebResponse) As String

            ' Begin by validating our inbound parameters.
            '
            If (response Is Nothing) Then

                Throw New ArgumentNullException("response", "Value cannot be null")
            End If

            ' Then, open up a reader to the response and read the contents to a string
            ' and return that to the caller.
            '
            Dim responseBody As String = ""
            Using rspStm As Stream = response.GetResponseStream()

                Using reader As StreamReader = New StreamReader(rspStm)

                    responseBody = reader.ReadToEnd()
                End Using
            End Using

            Return responseBody
        End Function 'ReadResponse'

        Private Function ReadPassword() As String

            Dim retval As StringBuilder = New StringBuilder()
            Dim keyInfo As ConsoleKeyInfo = Console.ReadKey(True)

            While keyInfo.Key <> ConsoleKey.Enter

                Console.Write("*")
                retval.Append(keyInfo.KeyChar)

                keyInfo = Console.ReadKey(True)
            End While
            Console.WriteLine()

            Return retval.ToString()
        End Function 'ReadPassword

    End Class 'Program
End Namespace 'DeleteEntityUsingREST

To verify that the entity was deleted, specify the entity URI in the Web browser and you should get an "entity not found" error.

https://<authority-id>.data.database.windows.net/v1/<container-id>/<entity-id>

You can specify an empty query with the specific container in the scope to retrieve all the entities in that container. For details about queries, see Querying SQL Data Services.

https://<authority-id>.data.database.windows.net/v1/<container-id>?q=''

See Also

Concepts

Deleting an Entity Using REST
Examples of Using SOAP and REST Interfaces with the SQL Data Services