public partial class _Default : System.Web.UI.Page 
{
    BaseClass bc = new BaseClass();
    int sum = 0;
    String sid = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["loginName"] == null)
        {
            Session["loginName"] = "";
        }
        sid = Session["loginName"].ToString();
        GridView1.DataSource = bc.GetDataSet("select tb_inscore.coname,score,mkscore,point from tb_inscore,tb_course where sid = '" + sid + "' and tb_inscore.coid=tb_course.coid", "tb_inscore");
        GridView1.DataBind();
        GridView2.DataSource = bc.GetDataSet("select tb_outscore.oname,point from tb_outscore,tb_outcourse where sid = '" + sid + "' and tb_outscore.oid=tb_outcourse.oid", "tb_outscore");
        GridView2.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)                   {           
            sum += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "score"));           
        }                 else if(e.Row.RowType == DataControlRowType.Footer)               {                    e.Row.Cells[2].Text="总分:";                         e.Row.Cells[3].Text =sum.ToString();              }    }
    protected void Button1_Click(object sender, EventArgs e)
    {        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        SqlCommand cmd = new SqlCommand("select count(*) from tb_students where sname='" + txtName.Text + "'and password='" + txtPwd.Text + "'", con);
        cmd.Connection.Open();
        int i = (int)cmd.ExecuteScalar();
        cmd.Connection.Close();
        if (i > 0)
        {
            Response.Write("登陆成功!!!");
            Session["loginName"] = txtName.Text;
        }
        else
        {
            Response.Write("失败!!!");
        }
        con.Close();    }
}为什么我登录之后session还是空的?不起作用啊

解决方案 »

  1.   

     sid = Session["loginName"].ToString(); 
            GridView1.DataSource = bc.GetDataSet("select tb_inscore.coname,score,mkscore,point from tb_inscore,tb_course where sid = '" + sid + "' and tb_inscore.coid=tb_course.coid", "tb_inscore"); //sid 是空值
      

  2.   

    谢谢楼上的,但是我 Button1_Click方法应该已经给Session["loginName"] 赋值了啊,为什么没有效果
      

  3.   

    在global里
    void Session_Start(object sender, EventArgs e) 
        {
             Session["loginName"] = ""; 
        }
    再调试看看
    Response.Write("登陆成功!!!"); 
    Session["loginName"] = txtName.Text; 
      

  4.   


    Button1_Click方法应该已经给Session["loginName"] 赋值的时候sid = Session["loginName"].ToString(); 
            GridView1.DataSource = bc.GetDataSet("select tb_inscore.coname,score,mkscore,point from tb_inscore,tb_course where sid = '" + sid + "' and tb_inscore.coid=tb_course.coid", "tb_inscore"); 已经都运行了 赋的值收不到
      

  5.   

    if (Session["loginName"] == null) //判断session是否存在

        Session["loginName"] = ""; //若不存在则创建并赋值
    } sid = Session["loginName"].ToString(); //赋值给sid,显然第一次运行时 sid为"" Session["loginName"] = txtName.Text; //给session赋值,所以,你点按钮时 sid还是为空,但是重新刷新该页面,sid就有值了建议测试一下
      在Session["loginName"] = txtName.Text;句后加Response.Write("session赋值成功");Response.Write("Session="+Session["loginName"]);
     看下输出结果就是了。
      

  6.   

    可以在Session["loginName"] = txtName.Text;句后加 sid = Session["loginName"].ToString(); 
    就ok了。
      

  7.   

    程序是先走的 protected void Page_Load(object sender, EventArgs e) ,再执行的protected void Page_Load(object sender, EventArgs e)。
      

  8.   


    在Response.Write("登陆成功!!!"); 
    Session["loginName"] = txtName.Text; 
    后面把GridView1.DataSource  重新绑定下 应该就可以了