Home > Back-end >  Can't Get Updated Value from Text box
Can't Get Updated Value from Text box

Time:10-31

I am working on a one project.and in this one of the page is UserProfile page when ever user click on the this page it's fetch the data from Dababase and it fill in the textbox. and if user want to update the data they can update. But the problem is it get old value from text box

Profile.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Content/MasterPage.master" AutoEventWireup="true" CodeFile="Profile.aspx.cs" Inherits="AdminSide_Profile" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link rel="stylesheet" href="../CSS/profilecss.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMain" runat="Server">
    <div >
        <div >
            <div >
                <div >
                    <%--<span >First User</span>--%>
                    <%--<span >[email protected]</span>--%>
                    <asp:Label ID="lblUser" runat="server" CssClass="font-weight-bold" EnableViewState="false"></asp:Label>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <h4 >Profile Settings</h4>
                    </div>
                    <div >
                        <div >
                            <label >Name</label>
                            <asp:TextBox ID="txtName" runat="server" CssClass="form-control" PlaceControl="Enter User Name"></asp:TextBox>
                            <%--<input type="text"  placeholder="Name" value="">--%>
                        </div>
                    </div>
                    <div >
                        <div >
                            <label >Mobile Number</label>
                            <asp:TextBox ID="txtMobileNumber" runat="server" CssClass="form-control" PlaceControl="Enter User Name"></asp:TextBox>
                        </div>
                        <div >
                            <label >Email ID</label>
                            <asp:TextBox ID="txtEmail" runat="server" CssClass="form-control" PlaceControl="Enter User Name"></asp:TextBox>

                        </div>
                        <div >
                            <label >Password</label>
                            <asp:TextBox ID="txtPassword" runat="server" CssClass="form-control" auto PlaceControl="Enter Password Name"></asp:TextBox>

                        </div>
                    </div>
                    <div >
                        <div >
                            <asp:Label ID="lblMessage" runat="server" EnableViewState="false"></asp:Label>
                        </div>
                    </div>
                    <div >
                        <%--<button  type="button">Save Profile</button>--%>
                        <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-primary profile-button" OnClick="btnSave_Click" />
                    </div>
                </div>
            </div>
        </div>
    </div>

</asp:Content>


Prfile.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InterviewPrepration.BAL;
using InterviewPrepration.ENT;
using InterviewPrepration.DAL;
using System.Data.SqlTypes;

public partial class AdminSide_Profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] != null)
        {
            UserBAL baluser = new UserBAL();
            UserENT entuser = new UserENT();

            entuser = baluser.SelectByID(Convert.ToInt32(Session["UserID"].ToString().Trim()));

            txtName.Text = entuser.User_Name.Value.ToString();
            txtMobileNumber.Text = entuser.ContactNo.Value.ToString();
            txtEmail.Text = entuser.Email_ID.Value.ToString();
            txtPassword.Text = entuser.Password.Value.ToString();
            lblUser.EnableViewState = true;
            lblUser.Text = entuser.User_Name.Value.ToString();
        }
        if (Session["UserID"] == null)
        {
            Response.Redirect("~/AdminSide/Login.aspx");
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        String err = "";
        if (txtName.Text.Trim() == "")
        {
            err  = "Please Enter Name";

        }
        if (txtEmail.Text.Trim() == "")
        {
            err  = "Please Enter EmailId";
        }
        if (txtMobileNumber.Text.Trim() == "")
        {
            err  = "Please Enter Mobile Number";
        }

        if (err != "")
        {
            lblMessage.EnableViewState = true;
            lblMessage.Text = err.ToString();
        }

        UserENT entuser = new UserENT();
        if(txtName.Text.Trim() != "")
        {
            entuser.User_Name = txtName.Text.Trim();
        }
        if (txtMobileNumber.Text.Trim() != "")
        {
            entuser.ContactNo = txtMobileNumber.Text.Trim();
        }
        if (txtEmail.Text.Trim() != "")
        {
            entuser.Email_ID = txtEmail.Text.Trim();
        }
        if(txtPassword.Text.Trim() != "")
        {
            entuser.Password = txtPassword.Text.Trim();
        }
        if(Session["UserID"] != null)
        {
            entuser.User_ID = Convert.ToInt32(Session["UserID"].ToString());
        }

        UserBAL baluser = new UserBAL();
        if (baluser.Update(entuser))
        {
            lblMessage.EnableViewState = true;
            lblMessage.Text = "Updated Sucessfully";
        }
    }
}

in specifie portion

protected void btnSave_Click(object sender, EventArgs e)
    {
      
        UserENT entuser = new UserENT();
        if(txtName.Text.Trim() != "")
        {
            entuser.User_Name = txtName.Text.Trim();
        }
        if (txtMobileNumber.Text.Trim() != "")
        {
            entuser.ContactNo = txtMobileNumber.Text.Trim();
        }
        if (txtEmail.Text.Trim() != "")
        {
            entuser.Email_ID = txtEmail.Text.Trim();
        }
        if(txtPassword.Text.Trim() != "")
        {
            entuser.Password = txtPassword.Text.Trim();
        }
        if(Session["UserID"] != null)
        {
            entuser.User_ID = Convert.ToInt32(Session["UserID"].ToString());
        } 
    }

BALUser.cs

 #region Update
    public Boolean Update(UserENT entuser)
    {
        UserDAL daluser = new UserDAL();
        if (daluser.PR_IP_User_UpdateByUserID(entuser))
        {
            return true;
        }
        else
        {
            Message = daluser.Message;
            return false;
        }
    }

This is a update method which is callby profile.aspx.cs

DALUser.cs DAL file which perfrom opation(Database).

#region PR_IP_User_UpdateByUserID
        public Boolean PR_IP_User_UpdateByUserID(UserENT entuser)
        {
            using (SqlConnection objConn = new SqlConnection(ConnectionString))
            {
                objConn.Open();
                using (SqlCommand objCmd = objConn.CreateCommand())
                {
                    try
                    {
                        #region PrpepredCommand
                        objCmd.CommandType = CommandType.StoredProcedure;
                        objCmd.CommandText = "PR_IP_User_UpdateByUserID";
                        objCmd.Parameters.AddWithValue("@UserID", entuser.User_ID);
                        objCmd.Parameters.AddWithValue("@UserName", entuser.User_Name);
                        objCmd.Parameters.AddWithValue("@Password", entuser.Password);
                        objCmd.Parameters.AddWithValue("@EmailID", entuser.Email_ID);
                        objCmd.Parameters.AddWithValue("@ContactNo", entuser.ContactNo);
                        //objCmd.Parameters.AddWithValue("@IsAdmin", entuser.Is_Admin);
                        #endregion

                        objCmd.ExecuteNonQuery();

                        return true;
                    }
                    catch (Exception ex)
                    {
                        Message = ex.InnerException.Message;
                        return false;
                    }
                }
            }

            #endregion

ENTUser.cs file

this is ent file of user.

namespace InterviewPrepration.ENT
{
    public class UserENT : DataBaseConfig
    {
        #region Constructor
        public UserENT()
        {

        }
        #endregion

        #region User ID
        protected SqlInt32 _User_ID;

        public SqlInt32 User_ID
        {
            get
            {
                return _User_ID;
            }
            set
            {
                _User_ID = value;
            }
        }
        #endregion

        #region User_Name
        protected SqlString _User_Name;

        public SqlString User_Name
        {
            get
            {
                return _User_Name;
            }
            set
            {
                _User_Name = value;
            }
        }
        #endregion


        #region User_Name
        protected SqlString _Password;

        public SqlString Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }
        #endregion

        #region User_Name
        protected SqlString _ContactNo;

        public SqlString ContactNo
        {
            get
            {
                return _ContactNo;
            }
            set
            {
                _ContactNo = value;
            }
        }
        #endregion

        #region User_Name
        protected SqlString _Email_ID;

        public SqlString Email_ID
        {
            get
            {
                return _Email_ID;
            }
            set
            {
                _Email_ID = value;
            }
        }
        #endregion


        #region IsAdmin
        protected SqlBoolean _Is_Admin;

        public SqlBoolean Is_Admin
        {
            get
            {
                return _Is_Admin;
            }
            set
            {
                _Is_Admin = value;
            }
        }
        #endregion


    }
}

CodePudding user response:

i tried if(!Page.IsPostBack).i put the if condition.Because it's fire before i click on the button.

This is the update code of: profile.aspx.cs

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InterviewPrepration.BAL;
using InterviewPrepration.ENT;
using InterviewPrepration.DAL;
using System.Data.SqlTypes;

public partial class AdminSide_Profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave.Attributes.Add("AutoPostback", "false");
        if (!Page.IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                UserBAL baluser = new UserBAL();
                UserENT entuser = new UserENT();

                entuser = baluser.SelectByID(Convert.ToInt32(Session["UserID"].ToString().Trim()));

                txtName.Text = entuser.User_Name.Value.ToString();
                txtMobileNumber.Text = entuser.ContactNo.Value.ToString();
                txtEmail.Text = entuser.Email_ID.Value.ToString();
                txtPassword.Text = entuser.Password.Value.ToString();
                lblUser.EnableViewState = true;
                lblUser.Text = entuser.User_Name.Value.ToString();
            }
        }
        if (Session["UserID"] == null)
        {
            Response.Redirect("~/AdminSide/Login.aspx");
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlString name, number, email, pass;
        SqlInt32 id;

        String err = "";
        if (txtName.Text.Trim() == "")
        {
            err  = "Please Enter Name";

        }
        if (txtEmail.Text.Trim() == "")
        {
            err  = "Please Enter EmailId";
        }
        if (txtMobileNumber.Text.Trim() == "")
        {
            err  = "Please Enter Mobile Number";
        }

        if (err != "")
        {
            lblMessage.EnableViewState = true;
            lblMessage.Text = err.ToString();
        }


        UserENT entuser = new UserENT();
        if (txtName.Text.Trim() != "")
        {
            entuser.User_Name = txtName.Text.Trim();
        }
        if (txtMobileNumber.Text.Trim() != "")
        {
            entuser.ContactNo = txtMobileNumber.Text.Trim();
        }
        if (txtEmail.Text.Trim() != "")
        {
            entuser.Email_ID  = txtEmail.Text.Trim();
        }
        if (txtPassword.Text.Trim() != "")
        {
            entuser.Password= txtPassword.Text.Trim();
        }
        if (Session["UserID"] != null)
        {
            entuser.User_ID = Convert.ToInt32(Session["UserID"].ToString());
        }


        

       


        UserBAL baluser = new UserBAL();
        if (baluser.Update(entuser))
        {
            lblMessage.EnableViewState = true;
            lblMessage.Text = "Updated Sucessfully";
        }

    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        txtName.ReadOnly = false;
        txtEmail.ReadOnly = false;
        txtMobileNumber.ReadOnly = false;
        txtPassword.ReadOnly = false;

    }
}
  • Related