This question may be very basic, but I have spent hours upon hours trying to correct the error. I made a web form in ASPX and c# where the authenticated user can edit their data such as name, surname, email, even change their access password. I have no problems with authentication, managing the session or even changing the password. all that works now. what does not work is to change the name and surnames, or the email. In theory it is very easy to save the textbox.text data, but the problem is that it does not save it, it keeps the previously loaded data. When loading the page, the current user data is loaded, and the idea is that you can change it, but it does not save the changed data, only the current one. For example, if my name is Jose Hernandez Rojas and I change to Jose Rojas Hernández, it keeps the first data, not the changed. I show you the ASPX code. It is important to indicate that I use a siteMater for the general interface
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table class="table table-bordered">
<tr>
<td>
</td>
<td >
<div style="max-width: 400px;">
<h2 class="form-signin-heading">Mi perfil</h2>
<asp:Label Text="Nombre completo" runat="server" AssociatedControlID="txtFullname"/>
<asp:TextBox ID="txtFullname" runat="server" CssClass="form-control" placeholder="Ingrese su nombre completo" required="required"/>
<br/>
<asp:Label Text="Nombre de usuario" runat="server" AssociatedControlID="txtUsername"/>
<asp:TextBox ID="txtUsername" runat="server" CssClass="form-control" placeholder="Enter Username" required="required" Enabled="False"/>
<br/>
<asp:Label Text="Correo electrónico" runat="server" AssociatedControlID="txtEmail"/>
<asp:TextBox ID="txtEmail" runat="server" TextMode="Email" CssClass="form-control" placeholder="Enter Email" required="required"/>
<br/>
<hr/>
<a href="ChangePassword.aspx" role="button" class="btn btn-primary">Cambiar la contraseña</a>
<asp:Button ID="btnSignup" runat="server" Text="Modificar mis datos" CssClass="btn btn-primary" OnClick="btnSignup_Click"/>
</div>
</td>
<td>
</td>
</tr>
</table>
</asp:Content>
This is the .cs code for this site, where I show that clicking the button triggers the action of updating the data. the SQL command works correctly, but the data returned by the txtFullname.text does not change, and that is my doubt. I already tried to put the property AutoPostBack="true" in the textboxes, but there is no change
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
namespace User_Login_CS
{
public partial class myProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("select * from users where Username='" + this.Page.User.Identity.Name + "';", con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
txtFullname.Text = reader["Fullname"].ToString();
txtUsername.Text = reader["Username"].ToString();
txtEmail.Text = reader["Email"].ToString();
int tipo = Convert.ToInt32(reader["RoleId"]);
}
}
con.Close();
}
}
}
protected void btnSignup_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Users SET Fullname='" + txtFullname.Text + "',Email='" + txtEmail.Text + "' WHERE Username='" + txtUsername.Text + "'", con))
{
string message = string.Empty;
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
message = "Usuario actualizado";
}
catch
{
message = "Ha habido un error";
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
}
}
protected void txtFullname_TextChanged(object sender, EventArgs e)
{
string nombre = txtFullname.Text;
}
protected void txtEmail_TextChanged(object sender, EventArgs e)
{
string correo = txtEmail.Text;
}
}
}
As you can see, I even tried with the TextChanged event but it didn't work either.
any other idea?? It's been a long time and I can't. Thank you!!
What happens is that in it you
Page_Load
always load the user data. When you give thebtnSignup
it executes first thePage_Load
and then thebtnSignup
, but inPage_Load
it always loads the user data and that is why it always sees the old ones.To solve it, enclose the block where you load the user data with an if:
The
!IsPostBack
is to ensure that it runs whenever the page first loads or that it doesn't come from an action within itself.