當我嘗試執行此函數時,出現錯誤 「System.InvalidCastException:指定的強制轉換無效。」C#請求處理程序錯誤System.InvalidCastException:指定的強制轉換無效
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
這是函數: 我嘗試用GUID作爲我的電子郵件地址和參數的援助與它的頭號參數來執行它。
請幫助我,我一直試圖整天修復它。
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 [email protected]";
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 [email protected]";
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);
}
}
哪一行是拋出異常?我不會計算'時間:) – SledgeHammer
@LaneL你可以,如果它確實是一個盒裝詮釋。如果OP無法告訴我們什麼是線路拋出異常,那麼猜測什麼是問題是沒有意義的。 – Jakotheshadows