简体   繁体   English

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

[英]List view cannot get user info from session

I have created in my login form a session which is UsrNme to get the user name. 我已经在登录表单中创建了一个名为UsrNme的会话来获取用户名。 It binds all the listviews and gridviews in the user panel page depending on the UsrNme Session. 它将根据UsrNme会话绑定用户面板页面中的所有列表视图和网格视图。 If the current user logs in with his username it will work normally, but in the registration form after a new user registers, the page shows nothing, even the user name. 如果当前用户使用其用户名登录将可以正常工作,但是在新用户注册后的注册表单中,该页面将不显示任何内容,即使用户名也是如此。 I have created another session in the registration form to collect the information of the user and bind the listviews depending on the new user name but it doesn't work. 我已经在注册表单中创建了另一个会话,以收集用户信息并根据新用户名绑定列表视图,但是它不起作用。

Here is login form 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";
    } 
}

Here is the registration form: 这是注册表格:

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;
}

Here is the user panel codebehind: 以下是用户面板代码:

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");
}

I think I understood your problem right. 我想我理解您的问题对。 In your registration page you have this code, which sets the user name in the session. 在您的注册页面中,您具有此代码,该代码用于设置会话中的用户名。 But why to you have "UsrNmeReg" instead of "UsrNme"?. 但是,为什么要使用“ UsrNmeReg”而不是“ UsrNme”? This is the session key that you are refering in your user panel code. 这是您在用户面板代码中引用的会话密钥。

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

I think you need to call the following line of code, in the btnSave_Click event before redirecting to the user panel page 我认为您需要在重定向到用户面板页面之前在btnSave_Click事件中调用以下代码行

Session["UsrNme"] =  UsrNme.Text

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM