0

When I am connecting to mysql in go daddy from my asp.net mvc application . I am getting bad request error. May I know how to debug it? The thing is when I tried the same having mysql in my local machine . It worked for me.The following is code from my html page.function

GetSupportedSports() {
            if (ajaxRequest != null) {
                ajaxRequest.abort();
            }
            var val =0;
       //     ajaxRequest = $.getJSON("http://localhost:8078/SportsClubDefault.svc/getsportslist/", null,
           ajaxRequest = $.getJSON("http://<mydomain>/sportsVD/SportsClubDefault.svc/getsportslist/", null,
            function (data) {
                $.each(data,function(i,item){
                    val = 1;
                    $('#sportsName').append("<div class='container' style='padding-top: 5%';><span>"+item.Name+"</span></div>")
            });

The following is the code form my mvc asp.net application

My Interface method

    [OperationContract]
    [WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetSportsList/")]
    List<Sport> GetSportsList();

the following code is from my implementation class

    public List<Sport> GetSportsList()
    {
        List<Sport> sports = new List<Sport>();
        sports = readSportsFromDB();
        return sports;

    }

In the following method , I am reading from database

private List<Sport> readSportsFromDB()
{

    MySqlConnection con = new MySqlConnection();
    string ConnectionString = "server=<go daddy database server>;user id=<database username > ;password=pwd;persistsecurityinfo=True;database=<database name>";
    con.ConnectionString = ConnectionString;

    MySqlCommand cmd = con.CreateCommand() as MySqlCommand;
    cmd.CommandText = "select * from supportedsports";
    try
    {
        con.Open();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }

    MySqlDataReader reader = cmd.ExecuteReader() as MySqlDataReader;
    List<Sport> sports = new List<Sport>();

    while (reader.Read())
    {
        Sport temp = new Sport();
        temp.Name = reader["sportName"].ToString();
        sports.Add(temp);

    }
    return sports;
}

I am getting the following error

Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

The following is the error screen shot . and one more point I want to make , I am able to attach the debugger , and found that it is not even hitting GetSportsList() in my code. enter image description here

mason
  • 31,774
  • 10
  • 77
  • 121
Gopi
  • 94
  • 1
  • 10
  • You are not properly closing your connections. Probably not the cause of the error, but still something you need to fix. You should read about how to handle objects that implement [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx) (MySqlConnection, MySqlCommand, MySqlDataReader) – mason Jan 26 '17 at 13:20
  • @mason , sure. Thanks for the link. yes I am aware of it and didn't close my connection as I am still doing POC and removed all unnecessary code to identity the issue. – Gopi Jan 26 '17 at 13:24
  • It's not just the connection, it's everything that implements IDisposable. And it's not just a matter of closing it. You need to either call close in a finally block or wrap the IDisposable object in a using statement. These sorts of issues are very difficult to locate after the fact, and it's important to fix these sorts of issues as early as possible. – mason Jan 26 '17 at 14:01
  • As for your actual question, you need to include more details about the error. Is your code reached at all? – mason Jan 26 '17 at 14:02
  • @mason. Sure. and you are 100% true. if I ignore these issues in early stage , then it will become difficult to solve in later stages. – Gopi Jan 26 '17 at 14:02
  • yes , my code is not hitting db at all I believe. Because , if instead of fetching data from DB , if I hardcode it , it is working. Also at the same time , if I use the same DB connection , in my console app , I am able to hit the db and fetch the data. – Gopi Jan 26 '17 at 14:06
  • You believe? You need to *know*. When debugging, you need to challenge all your assumptions and prove exactly what's happening. If you're not able to attach a debugger and step through the code, then at least add sufficient logging so that you can tell what's going on. – mason Jan 26 '17 at 14:07
  • yes , I am able to attach the debugger . in try { con.open() } , it is going to exception with bad request error as reason. – Gopi Jan 26 '17 at 14:24
  • Please edit your question to include those details. That's important to include up front. Give the entire error details (Exception class, message, inner exceptions etc) and state which line caused it. – mason Jan 26 '17 at 14:29
  • yes mason . I have added the error screen shot as well just now. – Gopi Jan 26 '17 at 14:52
  • Why did you tag this as ASP.NET MVC? Looks more like WCF to me. And the error appears to have nothing to do with MySQL or shared hosting, error appears to be more to do with the AJAX -> WCF. – mason Jan 26 '17 at 14:55
  • as it will get broader view from all asp.net people as well. true , but in that getsportsList() , if I return hard coded values instead of fetching from DB , it is working. hence I feel , the reason is related to mysql . – Gopi Jan 26 '17 at 14:59
  • I think you should add a try/catch block around `sports = readSportsFromDB();` and log any errors. – mason Jan 26 '17 at 15:46
  • I am getting the following error Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. – Gopi Jan 26 '17 at 16:38
  • Add that to your question. – mason Jan 26 '17 at 16:38
  • Thanks mason , that link helped me. The reason is trust level. – Gopi Jan 26 '17 at 17:22

0 Answers0