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);
}
答案 0 :(得分:1)
看起来你忘了打开连接
con.Open();
运行命令
cmd.ExecuteNonQuery();
然后关闭连接
con.Close();
答案 1 :(得分: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();
}
}
答案 2 :(得分:1)
您的代码没有显示将任何数据提交回其数据源的迹象。
您需要一个数据适配器,并且需要将其Insert命令设置为上面的命令。
SQLDataAdapter adapt = new SQLataAdapter();
然后你需要打开一个连接: -
conn.open();
adapt.UpdateCommand = cmd;
adapt.UpdateCommand.ExecuteNonQuery()
conn.close();
希望这有助于。
答案 3 :(得分: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.
}