列表视图无法从会话中获取用户信息

时间:2014-06-26 11:56:49

标签: c# asp.net session

我在登录表单中创建了一个UsrNme会话来获取用户名。它根据UsrNme会话绑定用户面板页面中的所有列表视图和网格视图。如果当前用户使用他的用户名登录,它将正常工作,但在新用户注册后的注册表单中,该页面不显示任何内容,甚至是用户名。我在注册表单中创建了另一个会话,以收集用户的信息并根据新的用户名绑定列表视图,但它不起作用。

这是登录表单codebehind:

protected void Loginbtn_Click(object sender, EventArgs e)
{
    SqlConnection log = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString);
    log.Open();
    string checkuser = "Select count(*) from UserInfo where UID='" + usrnamlogintxtbx.Text + "'";
    SqlCommand cmd = new SqlCommand(checkuser, log);
    int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString()); 
    log.Close();

    if (temp == 1)
    {
        log.Open();
        string checkpasswordquery = "Select Password from UserInfo where  UID='" + usrnamlogintxtbx.Text + "'";
        SqlCommand passcom = new SqlCommand(checkpasswordquery, log);
        string password = passcom.ExecuteScalar().ToString().Replace(" ","");

        if (password == usrnamloginpassbx.Text)
        {
            Session["UsrNme"] = usrnamlogintxtbx.Text;
            Response.Redirect("User panel.aspx");
        } 
        else
        {
            passwronglbl.Text = "Password is incorrect";
        }
    } 
    else
    { 
        wronglogusernamelbl.Text = "Invalid User Name";
    } 
}

以下是注册表格:

string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();

protected void Page_Load(object sender, EventArgs e)
{ 
    if (!Page.IsPostBack)
    {
        DataTable countrycascd = new DataTable();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
        {
            SqlDataAdapter adaptar = new SqlDataAdapter("select [countryID],[country] FROM [countr]", con);
            adaptar.Fill(countrycascd);

            countrdrdolst.DataSource = countrycascd;
            countrdrdolst.DataTextField = "country";
            countrdrdolst.DataValueField = "countryID";
            countrdrdolst.DataBind();
        } 
        countrdrdolst.Items.Insert(0, new ListItem("Välj land", "0"));
    }
} 

protected void btnSave_Click(object sender, EventArgs e)
{ 
    SqlConnection cn = new SqlConnection(sc);
    SqlCommand cmd = new SqlCommand();

    string sqlstatment = "INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img, Logo,RegDate) VALUES (@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@Logo,@RegDate)";

    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = sqlstatment;

    //Insert the parameters first
    cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
    cmd.Parameters.AddWithValue("@FN", fnbox.Text);
    cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
    cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
    cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
    cmd.Parameters.AddWithValue("@Email", emailbox.Text);
    cmd.Parameters.AddWithValue("@Country", countrdrdolst.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@State", statedrdolst.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@City", citiesdrdolst.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@Post", postbox.Text);
    cmd.Parameters.AddWithValue("@Img", persimgFileUpload1.FileName);
    cmd.Parameters.AddWithValue("@Logo", logoFileUpload.FileName);

    //Get the Current Date Time here
    cmd.Parameters.AddWithValue("@RegDate", DateTime.Now);

    if (!string.IsNullOrEmpty(UsrNme.Text))
    {
        Lblcheckusername.Text = "User Name Already Exist";
        Lblcheckusername.ForeColor = System.Drawing.Color.Red;
    }
    else
    {
        Lblcheckusername.Text = "User Name Available";
        Lblcheckusername.ForeColor = System.Drawing.Color.Green;
    } 
    if (persimgFileUpload1.HasFile)
    { 
        persimgFileUpload1.SaveAs(Server.MapPath("~/images/users/" + persimgFileUpload1.FileName)); 
    } 
    if (logoFileUpload.HasFile)
    { 
        logoFileUpload.SaveAs(Server.MapPath("~/images/Logos/" + logoFileUpload.FileName)); 
    } 
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    ad.SelectCommand = cmd;
    ad.Fill(ds);
    Response.Redirect("User panel.aspx");
} 

protected void UsrNme_TextChanged(object sender, EventArgs e)
{
    Session["UsrNmeReg"] = UsrNme.Text;
}

以下是用户面板代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["UsrNme"] != null)
    {
        USRNMElbl.Text = Session["UsrNme"].ToString();

        using (SqlConnection userlistview = new SqlConnection(sc))
        {
            // Open your connection
            userlistview.Open();

            // Build your data adapter
            SqlDataAdapter userinfoDA = new SqlDataAdapter("SELECT * FROM [UserInfo] WHERE ([UID] = @UID)", sc);

            // Grab your location (guaranteed to exist from the above if-statement
            string UsrNme = Convert.ToString(Session["UsrNme"]);

            // Add your parameters to your data adapter
            userinfoDA.SelectCommand.Parameters.AddWithValue("@UID", UsrNme);

            // Define your data set
            DataSet userinfods = new DataSet();

            // Fill your data set
            userinfoDA.Fill(userinfods);

            // Bind your results
            userinfo.DataSource = userinfods.Tables[0];
            userinfo.DataBind();
        }
    }
    else
    {
        return;
    }
}


protected void addadsbtn_Click(object sender, EventArgs e)
{            
    Guid newGUID = Guid.NewGuid();
    SqlConnection cn = new SqlConnection(sc);
    SqlCommand cmd = new SqlCommand();

    string sqlstatment = "INSERT INTO [ads] ([Section], [Category], [UID], [AdsTit], [AdsDesc], [Country], [State], [City], [AdsPrice], [Img1], [img2], [img3], [img4], [img5], [Wtags]) VALUES (@Section, @Category, @UID, @AdsTit, @AdsDesc, @Country, @State, @City, @AdsPrice, @Img1, @img2, @img3, @img4, @img5, @Wtags)";

    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = sqlstatment;

    //Insert the parameters first
    cmd.Parameters.AddWithValue("@Section", Catedrdoads.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@Category", SubCatedrdoads.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@UID", USRNMElbl.Text);
    cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
    //cmd.Parameters.AddWithValue("@AdsDesc", Editor1.Text);
    cmd.Parameters.AddWithValue("@Country", countrdrdolst.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@State", statedrdolst.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@City", citiesdrdolst.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@AdsPrice", adsaddpristxtbx.Text);
    cmd.Parameters.AddWithValue("@Img1", FileUpload1.FileName);
    cmd.Parameters.AddWithValue("@Img2", FileUploadImg2.FileName);
    cmd.Parameters.AddWithValue("@Img3", FileUploadImg3.FileName);
    cmd.Parameters.AddWithValue("@Img4", FileUploadImg4.FileName);
    cmd.Parameters.AddWithValue("@Img5", FileUploadImg5.FileName);
    cmd.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);

    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    ad.SelectCommand = cmd;
    ad.Fill(ds);

    Response.Redirect("User panel.aspx");
}

1 个答案:

答案 0 :(得分:1)

我想我理解你的问题是正确的。 在您的注册页面中,您有此代码,该代码在会话中设置用户名。但为什么你有" UsrNmeReg"而不是" UsrNme"?这是您在用户面板代码中引用的会话密钥。

protected void UsrNme_TextChanged(object sender, EventArgs e)
{
    Session["UsrNmeReg"] = UsrNme.Text;
}

我认为您需要在重定向到用户面板页面之前在btnSave_Click事件中调用以下代码行

Session["UsrNme"] =  UsrNme.Text