我如何传递会话值并将其放在文本框中?

时间:2015-07-01 05:11:29

标签: c# asp.net textbox session-variables

这是我的登录按钮点击代码。我已将会话["用户名"]设置为txtUser.text中客户的输入。

protected void btn_Login_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        conn.Open();
        string checkuser = "select count(*) from UserData where Username = '" + txtUser.Text + "'";
        SqlCommand scm = new SqlCommand(checkuser, conn);
        int temp = Convert.ToInt32(scm.ExecuteScalar().ToString());
        conn.Close();
        if (temp == 1)
        {
            conn.Open();
            string checkPassword = "select Password from UserData where Username ='" + txtUser.Text + "'";
            SqlCommand passCom = new SqlCommand(checkPassword, conn);
            string password = passCom.ExecuteScalar().ToString().Replace(" ", "");
            if (password == txtPassword.Text)
            {
                Session["Username"] = txtUser.Text;
                Response.Write("<script>alert('Record saved successfully')</script>");
                Response.Redirect("OrderNow.aspx");
            }
            else
            {
                lblcrederror.Text = ("Credentials dont match");
            }

这是我称之为的地方。 (ordernow.aspx)这是客户在他/她下订单时重定向的地方。我计划在提交订单之前将客户的价值(电子邮件地址用户名电话号码)传递到文本框中。

private void GetMyCart()
    {
        DataTable dtProducts; // declare data table = dtProducts.
        if (Session["MyCart"] != null) // check whether session is null or not.
        {
            dtProducts = (DataTable)Session["MyCart"]; //if session is not null, assign all session to dtproducts.
        }
        else
        {
            dtProducts = new DataTable(); //if session is null, create new datatable (dtproducts).
        }
        if (dtProducts.Rows.Count > 0) // if rows.count is greater than 0, it means there is a value records from the session.
        { 
            txtCustomerName.Text = Session["Username"].ToString();
            //txtCustomerPhoneNo.Text = Session["Contact"].ToString();
            //txtCustomerEmailID.Text = Session["Email"].ToString();
            //txtCustomerAddress.Text = Session["DeliveryAddress"].ToString();
            txtTotalProducts.Text = dtProducts.Rows.Count.ToString(); // this will display all of the chosen records
            btnIslandGas.Text = dtProducts.Rows.Count.ToString();
            dlCartProducts.DataSource = dtProducts;
            dlCartProducts.DataBind();
            UpdateTotalBill();

            pnlMyCart.Visible = true;
            pnlCheckOut.Visible = true;
            pnlEmptyCart.Visible = false;
            pnlCategories.Visible = false;
            pnlProducts.Visible = false;
            pnlOrderPlaceSuccessfully.Visible = false;

        }
        else // session is empty
        {
            pnlEmptyCart.Visible = true; // since session is empty and there is no value record, pull up the empty shopping cart page
            pnlMyCart.Visible = false;
            pnlCheckOut.Visible = false;
            pnlCategories.Visible = false;
            pnlProducts.Visible = false;
            pnlOrderPlaceSuccessfully.Visible = false;

            dlCartProducts.DataSource = null;
            dlCartProducts.DataBind();
            txtTotalProducts.Text = "0"; // total products, price and number logo is set to 0.
            txtTotalPrice.Text = "0";
            btnIslandGas.Text = "0";
        }

会话[&#34;用户名&#34;]正在运行。意思是它与txtCustomername.text捆绑在一起。但其余的都没有用(电子邮件,地址,电话号码)

1 个答案:

答案 0 :(得分:0)

据我所知,您正在做的是在您的登录页面上,以防用户进行身份验证,即在密码成功匹配时在您的代码中。会话变量即。根本没有设置联系人,电子邮件,DeliveryAddress。仅设置名称。

在此之后,您将重定向到ordernow.aspx页面。因此,你没有把它们带到那里。你只能得到一个。

在注册页面中,您可以设置其他会话变量,但您必须了解它之后才能在ordernow.aspx中使用

因此,如果您从注册到ordernow.aspx,您将获得值,但不会从登录页面转到ordernow.aspx

在重定向到ordernow页面并在那里访问它们之前,你需要在Login页面中设置其他Session变量。

更新:

您只是根据用户名从数据库获取密码,而是需要获取包含电子邮件,联系人,地址等其他详细信息的整个用户记录。然后匹配密码,如果匹配,则表示您拥有用户以及您需要设置会话变量的所有其他详细信息。

更新第二次:

if (temp == 1)
{
   conn.Open();
   string checkPassword = "select * from UserData where Username ='" + txtUser.Text + "'";
   SqlCommand passCom = new SqlCommand(checkPassword, conn);
   using (SqlDataReader oReader = passCom.ExecuteReader())
   {
      while (oReader.Read())
      {
        if(oReader["UserName"].ToString().Replace(" ", "") == txtPassword.Text.Trim())
        {
           Session["Username"]  = oReader["FirstName"].ToString();
           Session["Contact"]  = oReader["Contact"].ToString(); 
           Session["Email"] = oReader["Email"].ToString();
           Session["DeliveryAddress"] = oReader["DeliveryAddress"].ToString();
           Response.Redirect("OrderNow.aspx");
        }
        else
        {
           lblcrederror.Text = ("Credentials dont match");
           break;
        }                      
      }
      myConnection.Close();
   }
}