5

I am working on ASP.NET Web API and when I ran this method I get this error An error has occurred.

public List<DeviceClass> CheckDevice(string device)
{

    devices = new List<DeviceClass>();

    using( connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand("uspCheckDeviceID", connection))
        {
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@sp", SqlDbType.VarChar).Value = device;
            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                if(reader.HasRows)
                {
                    if(reader.Read())
                    {
                        DeviceClass model = new DeviceClass();
                        model.DeviceId = reader.GetValue(0).ToString();
                        model.Name = reader.GetValue(1).ToString();
                        model.Owner = reader.GetValue(2).ToString();

                        devices.Add(model);
                    }
                }

                connection.Close();
            }

        }
    }

    return devices;
}

What I am trying to determine if its code or if its the server connection.

This is the stored procedure I got an example:

declare @sp varchar(30)
set @sp ='marksiphoneid'
exec uspCheckDeviceID @sp 

Is there something wrong with the way I am trying to get results from my stored procedure?

The error is

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

DavidG
  • 113,891
  • 12
  • 217
  • 223
user979331
  • 11,039
  • 73
  • 223
  • 418
  • 5
    Are you going to tell us what error occurred or should we use our collective psychic powers? :) – DavidG Sep 25 '15 at 00:48
  • 2
    You still haven't provided us with the error from the server. 500 Internal Server Error means you need to figure out what the server side error is. – mason Sep 25 '15 at 00:52
  • I dont believe I have access to that information – user979331 Sep 25 '15 at 00:58
  • Put a breakpoint on the first line of your `CheckDevice()` method in VS. Hit the endpoint somehow (say, from the browser) and then debug your code. No one is going to be able to tell you what the cause of a 500 error is - it's internal to your code, and only you can determine what the underlying error is. – Brendan Green Sep 25 '15 at 01:54
  • How about using a simple try-catch block to capture the exception and check the details? – TejSoft Sep 25 '15 at 03:43
  • Add a try-catch as @TejSoft suggested, and also enable error detail policy in webapiconfig.cs. See this [link](http://stackoverflow.com/a/23374626/789264). – sid Mar 03 '16 at 13:25

2 Answers2

5

Turn your Method into this:

public HttpResponseMessage CheckDevice(string device)
{
    try
    {
        devices = new List<DeviceClass>();

        using (connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand("uspCheckDeviceID", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@sp", SqlDbType.VarChar).Value = device;
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        if (reader.Read())
                        {
                            DeviceClass model = new DeviceClass();
                            model.DeviceId = reader.GetValue(0).ToString();
                            model.Name = reader.GetValue(1).ToString();
                            model.Owner = reader.GetValue(2).ToString();

                            devices.Add(model);
                        }
                    }
                    connection.Close();
                }
            }
        }
        return Request.CreateResponse(HttpStatusCode.OK,
                devices.ToList());
    }
    catch (Exception e)
    {
        //Request.CreateErrorResponse can do the same thing
        var err = new HttpRequestMessage().
                    CreateErrorResponse(HttpStatusCode.InternalServerError,
                    e.ToString());

        return new HttpResponseException(err);
    }
}

Then you can get your Exception ToString when the Error Occured.

To Ensure you can see the Exception Message, you can use a Fiddler or PostMan To test your API.

.


Besides, I just try the way in the OP's question comment,

In my case I'm testing returning some Exception Object defined by myself,

originally when I call that GET Method through enter url to Google Chrome,

Chrome just give me a meaningless meesage An error has occured,

and after I add

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

at Global.asax.cs in the method protected void Application_Start(),

my Exception Object showed correctly.

.


An hour later I figure out that it might also could be done in WebApiConfig.cs in the method public static void Register(HttpConfiguration config) by

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
0

You 500 Error means you have internal error with your service. It might be everything inside.

For example, your reader.GetValue(1) returns null. So, you will have null error and your server will return 500.

alerya
  • 3,125
  • 3
  • 28
  • 50