C#REQUEST HANDLER ERROR System.InvalidCastException:指定的强制转换无效

时间:2016-08-18 20:15:47

标签: c# requesthandler

当我尝试执行此功能时,我收到错误 " System.InvalidCastException:指定的强制转换无效。"

chmod o+x

这是功能: 我尝试用参数guid执行它作为我的电子邮件地址和参数辅助,其中包含第一个。

请帮助我,我一整天都在努力解决它。

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

3 个答案:

答案 0 :(得分:1)

根据您提供的有限信息,问题出现在行cmd.Parameters.AddWithValue("@accId", (int) id);上,因为这是唯一一个有投射的行。

检查数据库中accounts.id列的类型,我怀疑它不是INT,因此您需要将转换(括号中的类型)更改为它的任何类型。 SMALLINT在C#中为shortBIGINTlongTINYINTbyte(或sbyte),您和#39;需要查看文档以查看MEDIUMINT映射到的内容,但可能int

答案 1 :(得分:1)

我注意到我在id之前有(int)但是它不需要所以我只删除了ID之前的(int)。

所以我可以替换这一行

AutoSize=true

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

答案 2 :(得分:0)

我猜哪条线正在抛出这个错误。

我可以看到使用你的id变量从对象到int发生的转换。

以下内容可能会显示您的问题

    [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;
    }

也许用int使int可以为空?或者看看对象的价值究竟是什么。

希望这有帮助。