0

I'm new on DotNet Framework (i'm using code igniter before), I try to post data into my SQL Server. This is my controller

 [HttpPost]
    public Response Post([FromBody] Log_Ups_User log_user)
    {
        Response response = new Response();
        try
        {
            LogAccessLayer log = new LogAccessLayer();
            log.AddUpsUser(log_user);                
            response.data = null;
            response.message = "Inserted";
            response.status = "Ok";

        }
        catch
        {

            response.data = null;
            response.message = "Error";
            response.status = "Sial";
        }
        return response;
    }

then my Response.cs

public class Response
{
    public string status { get; set; }
    public object data { get; set; }
    public string message { get; set; }
}

I post the data using Postman and it return

"status": "Sial", "data": null, "message": "Error"

But I don't know which part or which line of my code that cause error. How to return error that can give me some information.

Thanks

1 Answer 1

1

Try to use Exceptional block into your code. From your example try to modify your code just likes this. So you can see error message..

 public Response Post([FromBody] Log_Ups_User log_user)
{
    Response response = new Response();
    try
    {
        LogAccessLayer log = new LogAccessLayer();
        log.AddUpsUser(log_user);                
        response.data = null;
        response.message = "Inserted";
        response.status = "Ok";

    }
    catch(Exceptional ex)
    {

        response.data = null;
        response.message = ex.ToString();
        response.status = "Sial";
    }
    return response;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, that's great. Read below link & try to use exceptional block inside every methods of your project. tutorialsteacher.com/csharp/csharp-exception-handling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.