1

rId页面中有两个文本框。一个用于 UserId,另一个用于电子邮件。两者都是从表 aspnet_membership 中检索到的数据并设置为“只读”。对于电子邮件文本框,它将更改为只读 = false。然后用户输入一个新的电子邮件,然后点击按钮保存。它应该使用新电子邮件更新表中的电子邮件,但遗憾的是没有进行任何更改。有人可以告诉我应该删除/添加什么以使其正常工作。这是我的代码。

 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);

}
4

4 回答 4

1

看起来你忘了打开连接

con.Open();

运行命令

cmd.ExecuteNonQuery();

然后关闭连接

con.Close();
于 2012-09-12T13:53:43.850 回答
1
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();
   }
 }
于 2012-09-12T14:08:07.203 回答
1

您的代码没有显示将任何数据提交回其数据源的迹象。

您需要一个数据适配器,并且需要将其插入命令设置为上述命令。

SQLDataAdapter adapt = new SQLataAdapter();

然后你需要打开一个连接: -

conn.open();

adapt.UpdateCommand = cmd;

adapt.UpdateCommand.ExecuteNonQuery()

conn.close();

希望这可以帮助。

于 2012-09-12T14:10:24.817 回答
0

您可以尝试在按钮单击事件中通过 Membership 类直接更新用户:

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.
}
于 2012-09-12T13:59:49.447 回答