日期:2014-05-17  浏览次数:20469 次

各位大侠帮忙解决一下c# session的问题
我在登陆页面的登陆按钮里面设置了把UserName放入Session里面
C# code
    public string MessageBox(string txt,string Url) 
    {
        string str;
        str = "<script language=javascript>alert('"+txt+"');location='"+Url+"'</script>";
        return str;
    }

C# code
  protected void Button1_Click(object sender, EventArgs e)
    {
        if (i == 1) 
        {
            if(bc.seleSQL("select count(*) from Usersmr where UserName='"+UserName.Text+"' AND UserPwd='"+UserPwd.Text+"'AND Roled='"+qx+"'"))
            {   
                Session["loginName"] = UserName.Text;
                Response.Write(bc.MessageBox("学生登陆成功","../student/Text.aspx"));
 
            }
            else
            {
                Response.Write(bc.MessageBox("登录失败,请输入正确的用户名或者密码","Login.aspx"));
            }
        }
        if(i==0)
        {
             if(bc.seleSQL("select count(*) from Usersmr where UserName='"+UserName.Text+"' AND UserPwd='"+UserPwd.Text+"'AND Roled='"+qxa+"'"))
            {
                 Session["loginName"] = UserName.Text;
                 Response.Write(bc.MessageBox("教师登陆成功","../teacher/teacher.aspx"));
    
            }
            else
            {
                Response.Write(bc.MessageBox("登录失败,请输入正确的用户名或者密码","Login.aspx"));
            }
        }
    }

现在跳转到第二个页面后我用pageload方法调用出来
C# code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class teacher_teacher : System.Web.UI.Page
{
    string a, b;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            a = Session["loginName"].ToString();
            //Label1.Text = a;
            DataBase bc = new DataBase();
            Response.Write(bc.MessageBox(a));
            //bc.createCon();
            //b=bc.getRead("select DepartmentId from Usersmr where UserName='"+a+"'");
            //Label2.Text = b;
        }
        catch (Exception z) { }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

但是出现了错误。


“/在线考试系统”应用程序中的服务器错误。
--------------------------------------------

输入字符串的格式不正确。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.FormatException: 输入字符串的格式不正确。

源错误: 

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  

堆栈跟踪: 


[FormatException: 输入字符串的格式不正确。]
  System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7467367
  System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
  System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46
  System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +297
  System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChang