簡體   English   中英

如何傳遞會話值並將其放入文本框?

[英]how can i pass the session value and put it in textboxes?

這是我的登錄按鈕點擊代碼。 我已經在txtUser.text中將session [“ Username”]設置為客戶的輸入。

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

Session [“ Username”]正在工作。 表示它與txtCustomername.text綁定。 但其余的都無法使用(電子郵件,地址,電話號碼)

據我了解,您正在做的是在登錄頁面上進行登錄,以防用戶通過身份驗證,即在密碼成功匹配后在您的代碼中進行身份驗證。 會話變量即。 聯系人,電子郵件,DeliveryAddress都沒有設置。 僅設置名稱。

之后,您將重定向到ordernow.aspx頁。 因此,您不會將它們送到那里。 您只能得到一個您設置的。

在注冊頁面中,您可以設置其他Session變量,但是您必須了解,只有在ordernow.aspx中它們才可用。

因此,如果您從注冊轉到ordernow.aspx,則將獲取值,但從登錄頁面轉到ordernow.aspx時,則不會。

您還需要在“登錄”頁面上設置其他Session變量,然后再重定向到ordernow頁面並在那里訪問它們。

更新:

您僅根據用戶名從數據庫中獲取密碼,但是您需要獲取整個用戶記錄以及其他詳細信息,例如電子郵件,聯系方式,地址。 然后,與密碼匹配(如果與之匹配),則您需要用戶和他的所有其他詳細信息來設置會話變量。

更新第二:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM