指定的转换无效。

来源:百度知道 编辑:UC知道 时间:2024/05/15 03:32:30
“/WebApplication2”应用程序中的服务器错误。
--------------------------------------------------------------------------------

指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 指定的转换无效。

源错误:

行 80: Session["Power"]=dr["Power"];
行 81: lbl_message.Text="欢迎您!"+Session["ID"];
行 82: if((char)Session["Power"]==2)
行 83: {
行 84: hl_course.Enabled=false;

源文件: c:\inetpub\wwwroot\webapplication2\default.aspx.cs 行: 82

堆栈跟踪:

[InvalidCastException: 指定的转换无效。]
WebApplication2.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication2\default.aspx.cs:82
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEven

Session获取的返回值本来就是String类型的,你在这里直接判断if(Session["Power"] == "2")就可以了,还可以提高程序的性能

Session["Power"]里存储的是什么类型
如果是int
if((int)Session["Power"] == 2)

如果是string
if((string)Session["Power"] == "2")

如果是char
if(((char)Session["Power"])=='2')

只要是值类型:
if(Session["Power"].ToString() == "2")

源文件: c:\inetpub\wwwroot\webapplication2\default.aspx.cs 行: 82 :

if((char)Session["Power"]==2)
换成
if(((char)Session["Power"])=='2')

试试