-1

When i try to execute this function i get the error "System.InvalidCastException: Specified cast is not valid."

System.InvalidCastException: Specified cast is not valid.
   at server.mihail.credits.HandleRequest() in F:\Users\Mihail\Documents\new-sentients-2016\server\mihail\credits.cs:line 34
   at server.RequestHandler.HandleRequest(HttpListenerContext context) in F:\Users\Mihail\Documents\new-sentients-2016\server\RequestHandlers.cs:line 37
   at server.Program.ProcessRequest(HttpListenerContext context) in F:\Users\Mihail\Documents\new-sentients-2016\server\Program.cs:line 156

This is the function: I try to execute it with parameters guid as my email address and parameter aid with the number one in it.

Please help me, i have been trying all day to fix it.

class credits : RequestHandler
{
    protected override void HandleRequest()
    {
        string status = "403";
        using (Database db = new Database())
        {
        NameValueCollection query = HttpUtility.ParseQueryString(Context.Request.Url.Query);
        MySqlCommand cmd = db.CreateQuery();
        cmd.CommandText = "SELECT id FROM accounts WHERE uuid=@uuid";
        cmd.Parameters.AddWithValue("@uuid", query["guid"]);
        object id = cmd.ExecuteScalar();
        if (id != null)
        {
        int amount = int.Parse(query["aid"]);
        cmd = db.CreateQuery();
        cmd.CommandText = "UPDATE stats SET credits = credits + @amount WHERE accId=@accId";
                cmd.Parameters.AddWithValue("@accId", (int) id);
                cmd.Parameters.AddWithValue("@amount", amount);
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                    status = "400";
                else
                    status = "500";
           }
            else
               status = "404";
        }
        byte[] res = Encoding.UTF8.GetBytes(
             status);
        Context.Response.OutputStream.Write(res, 0, res.Length);
    }
}
  • 2
    which line is it throwing the exception on? I'm not going to count 'em :) – SledgeHammer Aug 18 '16 at 20:16
  • @LaneL you can if it is indeed a boxed int. There is no point guessing at what the problem is if the OP can't be bothered to tell us what line throws the exception. – Jakotheshadows Aug 18 '16 at 20:21

3 Answers3

1

I noticed that i have (int) before id but it isn't needed so i only removed the (int) before ID.

So i can replace this line

cmd.Parameters.AddWithValue("@accId", (int) id);

with

cmd.Parameters.AddWithValue("@accId", id);
0

Based on the limited information you have provided, it looks like the problem is with the line cmd.Parameters.AddWithValue("@accId", (int) id);, as that is the only line with a cast.

Check the type of the accounts.id column in your database, I suspect it is not an INT, so you will need to change the cast (the type in the parentheses) to whatever type it is. A SMALLINT is a short in C#, BIGINT is long, TINYINT is byte (or sbyte), and you'd need to look at the documentation to see what MEDIUMINT maps to, but its probably int.

RoadieRich
  • 6,330
  • 3
  • 35
  • 52
0

I am guessing which line is tossing this error.

I can see a cast happening from object to int with your id variable.

The following could show your problem

    [TestMethod]
    public void ObjectToInt()
    {
        object a = 2;
        object b = "3";
        int aa = (int)a;
        int bb = (int)b; // this throws the same error
        var x = 1;
    }

Maybe make the int nullable with int? or see what the value of object really is.

Hope this helps.

TheNoob
  • 861
  • 2
  • 11
  • 25