abokor hassan
abokor hassan

Reputation: 357

deleting data from webservice not showing anything? XAMARIN

I have been trying to delete a data using "DeleteAsync" and it doesn't show anything neither an error, when i hit delete buttom nothing happens. although things seems fine to me, but you guys help where i missed?

this is the code

private async void Delete(object sender, EventArgs e)
    {
        private const string weburl = "http://localhost:59850/api/Donate_Table";
        var uri = new Uri(string.Format(weburl, txtID.Text));
        HttpClient client = new HttpClient();
        var result = await client.DeleteAsync(uri);
        if (result.IsSuccessStatusCode)
        {
            await DisplayAlert("Successfully", "your data have been Deleted", "OK");
        }
    }

Upvotes: 1

Views: 250

Answers (1)

Nkosi
Nkosi

Reputation: 247443

Your web API url appears to be wrong as the weburl is set using

private const string weburl = "http://localhost:59850/api/Donate_Table";
var uri = new Uri(string.Format(weburl, txtID.Text));

Note the missing placeholder in the weburl yet it is being used in a string.Format(weburl, txtID.Text

From that it would appear that the weburl was probably meant to be

private const string weburl = "http://localhost:59850/api/Donate_Table/{0}";

so that the id of the resource to be deleted will be part of the URL being called.

Also it is usually suggested that one avoid repeatedly creating instances of HttpClient

private static HttpClient client = new HttpClient();
private const string webUrlTempplate = "http://localhost:59850/api/Donate_Table/{0}";
private async void Delete(object sender, EventArgs e) {        
    var uri = new Uri(string.Format(webUrlTempplate, txtID.Text));        
    var result = await client.DeleteAsync(uri);
    if (result.IsSuccessStatusCode) {
        await DisplayAlert("Successfully", "your data have been Deleted", "OK");
    } else {
        //should have some action for failed requests.
    }
}

Upvotes: 2

Related Questions