0

Well, after a week of trying just about everything I could find on here, and elsewhere, to resolve this problem, I'm finally flying the flag of surrender and opening this up to my better experienced fellows. Basically I have a simple intranet app that resides on a VMed Windows 2012 R2/IIS8 server, which also hosts SQL 2014(I know, I know). I'm trying to autocomplete on two textboxes from a SQL table. In VS2017 it works perfectly fine, but when I try running it from IIS8 I get a "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" error. Here's the HTML:

<script type="text/javascript">
                    $(function () {
                        $("#<%=txtSAPLoc.ClientID%>").autocomplete({
                            source: function (request, response) {
                                var param = { locName: $('#<%=txtSAPLoc.ClientID%>').val() };
                                $.ajax({
                                    method: "POST",
                                    url: "CdtCreateRecord.aspx/GetLoc",
                                    data: JSON.stringify(param),
                                    dataType: "json",
                                    contentType: "application/json; charset=utf-8",
                                    dataFilter: function (data) { return data; },
                                    success: function (data) {
                                        response($.map(data.d, function (item) {
                                            return {
                                                value: item
                                            }
                                        }))
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        alert(errorThrown);
                                    }
                                });
                            },
                            minLength: 2
                        });

                    });

                    $(function () {
                        $("#<%=txtSAPInst.ClientID%>").autocomplete({
                            source: function (request, response) {
                                var param = { instName: $('#<%=txtSAPInst.ClientID%>').val() };
                                $.ajax({
                                    method: "POST",
                                    url: "CdtCreateRecord.aspx/GetInst",
                                    data: JSON.stringify(param),
                                    dataType: "json",
                                    contentType: "application/json; charset=utf-8",
                                    dataFilter: function (data) { return data; },
                                    success: function (data) {
                                        response($.map(data.d, function (item) {
                                            return {
                                                value: item
                                            }
                                        }))
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        alert(errorThrown);
                                    }
                                });
                            },
                            minLength: 2
                        });

                    });
</script>

And the relevant code behind:

        [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static List<string> GetLoc(string locName)
    {
        List<string> Loc = new List<string>();
        string query = string.Format("SELECT DISTINCT loc FROM Locations WHERE loc LIKE '%{0}%'", locName);

        using (SqlConnection con = new SqlConnection("Server=usmac2dgsyntax;Database=LREC_ADB;Trusted_Connection=Yes;"))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Loc.Add(reader.GetString(0));
                }
            }
        }
        return Loc;
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static List<string> GetInst(string instName)
    {
        List<string> Inst = new List<string>();
        string query = string.Format("SELECT DISTINCT inst FROM Institutions WHERE inst LIKE '%{0}%'", instName);

        using (SqlConnection con = new SqlConnection("Server=usmac2dgsyntax;Database=LREC_ADB;Trusted_Connection=Yes;"))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Inst.Add(reader.GetString(0));
                }
            }
        }
        return Inst;
    }

This is a sampling of stackoverflow articles I previously consulted:

  • Can you not put a debug point in your c# and step through to see where the internal error is occuring? – Pete May 03 '18 at 13:59
  • Have you given the user relevant permissions in IIS? – bilpor May 03 '18 at 14:01
  • it is not a IIS problem – aas May 03 '18 at 14:11
  • have you tried after removing contentType: "application/json; charset=utf-8" part ? – aas May 03 '18 at 14:12
  • @Pete Sorry for seeming so dim, but how do you put a debug point in a production dll? I honestly have never done that. This is working fine in VS, buy not in the IIS deployed release. – Jamie Piscitelli May 03 '18 at 15:04
  • https://learn.microsoft.com/en-gb/visualstudio/debugger/remote-debugging or check the server error logs - there should be details of the exception in there – Pete May 03 '18 at 15:05
  • @bilpor Permissions are set correctly, and it's only me trying it so far, so...If it is an IIS problem, I'm just not seeing it. – Jamie Piscitelli May 03 '18 at 15:06
  • @aas Just tried it. Now instead of a 500 error, it returns "SyntaxError: Unexpected token < in JSON at position 4" – Jamie Piscitelli May 03 '18 at 15:08
  • @Pete Thanks much for the info! The IIS error logs note the 500 internal error, but not much else. "500.00 64," etc. – Jamie Piscitelli May 03 '18 at 15:13
  • @Pete...Thanks again for the info. When remote debugging the live site I picked up on the fact that the 500 error was owed to a SQL authentication error. I had neglected to give NT AUTHORITY/NETWORK SERVICE datareader/datawriter permissions on the relevant DB. Problem solved. Live and learn. Thanks everyone for the input. – Jamie Piscitelli May 07 '18 at 13:18

1 Answers1

0

In the end the, solution to my problem was maddeningly simple: After setting up remote debugging I found that a SQL authentication error was, in turn, causing the 500 internal server error. In SQL I had assumed that the NT AUTHORITY\NETWORK SERVICE account had the correct datareader/datawriter permissions on the relevant DB, but they had never been assigned, hence the authentication error and the internal server error in production. I fixed that, and it all works now. Thanks for the tips.