Clarify Seeker
Clarify Seeker

Reputation: 117

Table not update with new data

rIdThere are two text boxes in page. One is for UserId and the other one is for email. Both are retrieved data from table aspnet_membership and are set 'read-only'. For email text box, it will change read-only = false. Then user get to enter a new email then hit button save. It should update the email in table with the new email but unfortunately no changes made. Can some one tell me what should I remove/add to make it works. Here is my code.

 protected void Page_Load(object sender, EventArgs e)
{

    string email = Membership.GetUser(User.Identity.Name).Email;
    MembershipUser currentUser = Membership.GetUser();
    string UserId = currentUser.ProviderUserKey.ToString();

    TextBox2.Text = email;
    TextBox3.Text = UserId;
}

 protected void Button4_Click(object sender, EventArgs e)
{
    TextBox2.ReadOnly = false;
}

protected void Button3_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    SqlCommand cmd = new SqlCommand("UPDATE aspnet_membership SET Email = @email WHERE UserName = @id1", conn);

    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@email", TextBox2.Text);
    cmd.Parameters.AddWithValue("@id1", TextBox3.Text);

}

Upvotes: 1

Views: 725

Answers (4)

Nudier Mena
Nudier Mena

Reputation: 3274

I have refatored your code, now it should work

protected void Button3_Click(object sender, EventArgs e){
SqlConnection conn = new   SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("UPDATE aspnet_membership SET Email = @email WHERE UserName = @id1", conn);

cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@email", TextBox2.Text);
cmd.Parameters.AddWithValue("@id1", TextBox3.Text);   
 try { 
conn.Open();
cmd.ExecuteNonQuery();

   }
  catch(Exception ex){ 
  throw ex;    
  }
finally{    
conn.Close();
   }
 }

Upvotes: 1

Derek
Derek

Reputation: 8630

You code is showing no signs of committing any data back to its Data Source.

You need a Data Adapter, and you need to set its Insert Command to the command above.

SQLDataAdapter adapt = new SQLataAdapter();

you then need to open a connection :-

conn.open();

adapt.UpdateCommand = cmd;

adapt.UpdateCommand.ExecuteNonQuery()

conn.close();

Hope This Helps.

Upvotes: 1

tranceporter
tranceporter

Reputation: 2261

You can try directly updating the user via Membership class in your button click event:

protected void Button3_Click(object sender, EventArgs e)
{
     var memUser = Membership.GetUser(TextBox3.Text) //Fetch the user by user Id
     memUser.Email = TextBox2.Text // Assign the new email address
     Membership.UpdateUser(memUser) // update the user record.
}

Upvotes: 0

Vytalyi
Vytalyi

Reputation: 1695

Look like you forgot to open connection

con.Open();

run command

cmd.ExecuteNonQuery();

and then close connection

con.Close();

Upvotes: 1

Related Questions