简体   繁体   中英

Problem with Textboxes control in asp.net web application. Didn't get text from textboxes for update

I have a form AdminHome.aspx . When a user signed in, then on AdminHome.aspx page Load Event, That users profile data loaded from database and populate corresponding Textboxes . I have a Button control for Update User Information . When a User logged in, this is how its Home Page looks like. Here is the picture . 在此处输入图片说明

Now when i change the Designation from INTERN to Trainee . Here is the picture 在此处输入图片说明 and click on Update My Profile Button. It shows no Errors or Exceptions , instead it display a message Record Update Successfully . But when i check it in the database, it wasn't updated. After putting it on debug mood , i came to know that its taking the older values from Textboxes , I mean i change the value of Designation from INTERN to TRAINEE but still its taking INTERN . Here is the picture 在此处输入图片说明 Following is my update Button Code

        protected void btnUpdateProfile_Click(object sender, EventArgs e)
    {
        try
        {
                UpdateUser();
        }
        catch (Exception ex)
        {
            ShowNotification("Error: " + ex + "", WarningType.Danger);
        }
    }

    private void UpdateUser()
    {
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {

            using (SqlCommand cmd = new SqlCommand("UPDATE TableUserProfile SET UserName=@UserName,UserContact=@UserContact,UserDesignation=@UserDesignation,UserDepartment=UserDepartment WHERE UserEmpNum=@UserEmpNum", con))
            {
                string Uname, UContact, UDesignation, UDepartment, UEmployeeNo;
                Uname = tbName.Value.ToUpper();
                UContact = tbMobileNo.Value.ToUpper();
                UDesignation = tbDesignation.Value.ToUpper();
                UDepartment = tbDepartment.Value.ToUpper();
                UEmployeeNo = tbEmployeeNo.Value.ToUpper();
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@UserName", Uname);
                cmd.Parameters.AddWithValue("@UserContact", UContact);
                cmd.Parameters.AddWithValue("@UserDesignation", UDesignation);
                cmd.Parameters.AddWithValue("@UserDepartment", UDepartment);
                cmd.Parameters.AddWithValue("@UserEmpNum", UEmployeeNo);
                con.Open();
                cmd.ExecuteNonQuery();
                ShowNotification("Succes: Record Saved Succesfully!", WarningType.Success);
            }
        }
    }

and here is the .aspx code.

 <asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server"> <div class="container"> <div class="row"> <div class="col-md-8"> <div class="card"> <div class="card-body"> <div class="row"> <div class="col-md-10"> <h4>Your Profile</h4> <hr /> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group row"> <label for="username" class="col-4 col-form-label">Name*</label> <div class="col-6"> <input runat="server" id="tbName" class="form-control here" required="required" type="text" /> </div> </div> <div class="form-group row"> <label for="name" class="col-4 col-form-label">Mobile Number</label> <div class="col-6"> <input runat="server" id="tbMobileNo" class="form-control here" type="text" /> </div> </div> <div class="form-group row"> <label for="lastname" class="col-4 col-form-label">Employee Number</label> <div class="col-6"> <input runat="server" id="tbEmployeeNo" class="form-control here" readonly="True" type="text" aria-readonly="True" aria-disabled="True" /> </div> </div> <div class="form-group row"> <label for="text" class="col-4 col-form-label">Designation</label> <div class="col-6"> <input runat="server" id="tbDesignation" class="form-control here" required="required" type="text" /> </div> </div> <div class="form-group row"> <label for="text" class="col-4 col-form-label">Department</label> <div class="col-6"> <input runat="server" id="tbDepartment" class="form-control here" required="required" type="text" /> </div> </div> <div class="form-group row"> <div class="offset-4 col-8"> <asp:Button runat="server" ID="btnUpdateProfile" Text="Update My Profile" class="btn btn-primary" OnClick="btnUpdateProfile_Click"></asp:Button> </div> </div> </div> </div> </div> </div> </div> </div> </div> </asp:Content> 

Here is the PageLoad code.

     protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserEmployee"] != null)
        {
            userEmployeeNumber = Convert.ToString(Session["UserEmployee"]);
            GetUserData();
            ShowNotification("Welcome! Mr/Mrs " + EmployeeID.UserName.ToString() + "", WarningType.Success);
        }
    }

    private void GetUserData()
    {
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT [UserName],[UserContact],[UserEmpNum],[UserDesignation],[UserDepartment] FROM TableUserProfile WHERE UserEmpNum=@UserEmpNum", con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("UserEmpNum", userEmployeeNumber);
                SqlDataReader r = cmd.ExecuteReader();

                while (r.Read())
                {
                    tbName.Value = r["UserName"].ToString();
                    EmployeeID.UserName = tbName.Value.ToString();
                    tbMobileNo.Value = r["UserContact"].ToString();
                    tbEmployeeNo.Value = r["UserEmpNum"].ToString();
                    tbDesignation.Value = r["UserDesignation"].ToString();
                    tbDepartment.Value = r["UserDepartment"].ToString();
                }
            }
        }
    }

In your Page_Load, you have to check IsPostBack

 protected void Page_Load(object sender, EventArgs e)
 {
       if(!Page.IsPostBack)   
       {
        if (Session["UserEmployee"] != null)
        {
            userEmployeeNumber = Convert.ToString(Session["UserEmployee"]);
            GetUserData();
            ShowNotification("Welcome! Mr/Mrs " + EmployeeID.UserName.ToString() + "", WarningType.Success);
        }
      }
 }

Otherwise in each pageload, your textbox data gets updated with DB value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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