简体   繁体   中英

Data not getting deleted using API from the SQL Server in xamarin forms. HttpClient delete

I'm developing a Xamarin Forms application where I have to delete the record from the database when the reset button is clicked. We have an API that does the action.

The code is as follows:

API code

[HttpDelete]
public Response ResetRecord(string pk)
{
    try
    {
        Response response = new Response();

        using (var ctxdel = new GDSEntities4())
        {
            var result = ctxdel.SpDeleteRecordOnReset(int.Parse(pk));

            if (result > 0)
            {
                response.Message = result.ToString();
                response.Status = 1;                        
            }
            else
            {
                response.Message = "0";
                response.Status = 0;
            }
        }

        return response;
    }
    catch (Exception ex)
    {
        Response res = new Response();
        res.Message = ex.Message;
        res.Status = 0;
        return res;
    } 
}

This is working when I check with postman. In the front end, the delete function is as follows:

public async Task DeleteRecFromDB(string pk)//(, bool isNewItem = false) 
{
    try
    {
        HttpClient client = new HttpClient();
        Response response = new Response();
        string url = "http://xx.xxx.xxx.xxx/api/GDS/ResetRecord?PK={pk}";               

        var responseData = await client.DeleteAsync(url);

        if (responseData.IsSuccessStatusCode)
        {
            string result = await responseData.Content.ReadAsStringAsync();
            response = JsonConvert.DeserializeObject<Response>(result);

            var message = response.Message;
            var status = response.Status;

            await DisplayAlert("Success","Deleted from DB","OK");
        }
        else
        {
            await DisplayAlert("Failed", "Couldnt Delete from DB", "OK");
        }
    }
    catch (Exception)
    {
        UserDialogs.Instance.HideLoading();
        await DisplayAlert(LangResource.Message, LangResource.MsgWentWrong, LangResource.ok);
    }
}

The value pk is coming to that function DeleteRecFromDB but I can't find what the problem is. It is executing Couldnt Delete from DB part. The record is not getting deleted from the database. But the stored procedure and the API are working fine. I am not able to delete the record from the front end.

The response when checked in postman

{
    "Message": "0",
    "Status": 0
}

My stored procedure is as follows:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SpDeleteRecordOnReset]
    (@ID int)
AS
BEGIN
    DELETE FROM ProfMst 
    WHERE PK = @ID
END

Assuming that pk is 123 , you are trying to emit the request

DELETE /api/GDS/ResetRecord?PK=123 HTTP/1.1
Host: xx.xxx.xxx.xxx

Anyway, the actual request you are sending is

DELETE /api/GDS/ResetRecord?PK={pk} HTTP/1.1
Host: xx.xxx.xxx.xxx

because you missed the $ for string interpolation in the line

string url = "http://xx.xxx.xxx.xxx/api/GDS/ResetRecord?PK={pk}";

Hence url has the value http://xx.xxx.xxx.xxx/api/GDS/ResetRecord?PK={pk} instead of http://xx.xxx.xxx.xxx/api/GDS/ResetRecord?PK=123 . You can easily fix this by changing the line to

string url = $"http://xx.xxx.xxx.xxx/api/GDS/ResetRecord?PK={pk}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM