Reputation: 35
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
Upvotes: 0
Views: 248
Reputation: 9732
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}";
Upvotes: 2