简体   繁体   English

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

[英]System.InvalidCastException: Specified cast is not valid C#

Can you please help with this error? 您能帮忙解决此错误吗? I followed a tutorial to make a forum for my project,however I'm getting the error stated above.Thank you for any suggestions. 我遵循了一个教程为我的项目创建论坛,但是我遇到了上述错误。谢谢您的任何建议。 Error line: 错误行:

Int64 Forum_Id = (Int64)GridView1.SelectedValue;

my code 我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Forum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string course_Id = DropDownList1.Text;
        int ccourse_Id = Convert.ToInt32(course_Id);
        string question = TextBox1.Text;
        string posterName = TextBox2.Text;
        DateTime blog_date = DateTime.Now;
        PostForum.INSERTforum(ccourse_Id, question, posterName, blog_date);

        GridView1.DataBind();
        }



    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Int64 Forum_Id = (Int64)GridView1.SelectedValue;


        Session["forum_Id"] = Forum_Id;


        Response.Redirect("Thread.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }
}

In my databse I have a table called Forum with forum_id that is Integer not null 在我的数据库中,我有一个名为Forum的表,带有forum_id,它是Integer不为null

尝试这个:

Int64 Forum_Id = Convert.ToInt64(GridView1.SelectedValue.ToString());

SelectedValue might not be an Int64. SelectedValue可能不是Int64。 Try parsing to get the numeric value. 尝试解析以获取数值。

Int64 Forum_Id;
Int64.TryParse( GridView1.SelectedValue.ToString(), out Forum_Id);

You can use following line if you are sure that the selected value contains only numeric characters. 如果您确定所选值仅包含数字字符,则可以使用以下行。

Int64 value = Int64.Parse(GridView1.SelectedValue.ToString()); Int64值= Int64.Parse(GridView1.SelectedValue.ToString());

Otherwise, it would throw an exception. 否则,它将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL C#System.InvalidCastException:指定的转换无效 - C# System.InvalidCastException: Specified Cast is not valid 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:'指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' C#System.InvalidCastException:'指定的转换无效。' 将MySQL与Discord.Net一起使用时 - C# System.InvalidCastException: 'Specified cast is not valid.' When using MySQL with Discord.Net C#请求处理程序错误System.InvalidCastException:指定的强制转换无效 - C# REQUEST HANDLER ERROR System.InvalidCastException: Specified cast is not valid System.InvalidCastException:使用postgres datareader在C#中指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid in C# using postgres datareader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM