Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I want to be able to update the details of the user that is logged in. So I have multiple attributes that I would like to update however I want the user not have to enter their ID each time they would like to change something. This is my code under the save button however nothing happens except redirect to the other page. Any help would be kindly appreciated

What I have tried:

<pre> protected void btnTenantSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Creating a connection to my database using the connection string
            string cs = System.Configuration.ConfigurationManager.ConnectionStrings["rent-dbConnectionString1"].ConnectionString;
        SqlConnection con = new SqlConnection(cs);
        //preparing a query which will update the data in the database with the data entered into the textboxes
        SqlCommand cmd = new SqlCommand("UPDATE Tenants SET Tenant_FullName='" + this.txtTenantFullName.Text + "',Tenant_Email='" + this.txtTenantEmail.Text + "',Tenant_TelNum='" + this.txtTenantTelNum.Text + "',Tenant_EmName= '" + this.txtTenantEmName.Text + "',Tenant_EmNum= '" + this.txtTenantEmNum.Text + "',Tenant_WorkStatus= '" + this.txtWorkstatus.Text + "'Where Tenant_Email='" + Session["TenantLogin"] + "';", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        }
        Response.Redirect("TenantIndex.aspx");
       
    }
Posted
Updated 30-Jan-18 14:31pm
Comments
an0ther1 30-Jan-18 18:40pm    
Do not use string concatenation to create SQL statements, you are leaving yourself open to SQL Injection, refer to the following links;
MSDN: Protect from SQL Injection
W3 Schools:SQL Injection

Secondly, use your debugger - does the TenantLogin session value exist? Is it populated?
If the answer is yes, then you will need to determine why you are not updating the correct record, this could be because of permissions or your statement, your debugger should be able to assist with this - refer below link;
MSDN: Debugging in Visual Studio

Kind Regards
F-ES Sitecore 31-Jan-18 5:00am    
If "nothing happens" then it is possible the "where" clause isn't matching a valid record in your database. We can't access your data and we don't know what is in Session["TenantLogin"] so we can't check that for you, only you can check that so learn to use the debugger to step through your code.

1 solution

When the user logs in you need to store their id in a Session variable and then you can use that anytime you need it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900