使用 ContentEditable 通过 ASP.NET 保存到数据库中

本文关键字:数据库 保存 NET ContentEditable 通过 ASP 使用 | 更新日期: 2023-09-27 18:30:49

>我正在尝试创建一个网页,用户可以在其中编辑文本,完成后,他们点击保存,输入的新文本将保存到数据库中。

我的代码中没有任何错误,但由于某种原因,旧文本只是被重写到 db 而不是新文本中。

这是我的代码隐藏:

protected void saveBtn_Click(object sender, EventArgs e)
{
    string newName;
    string newIntro;
    string newEduc;
    string newWork;
    h1New.Text = h1.Text;
    newName = h1New.Text;
    newIntro = intro.Text;
    newEduc = educ.Text;
    newWork = employ.Text;
    string connectionInfo = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionInfo))
    {
        connection.Open();
        SqlCommand myCommand = new SqlCommand("UPDATE simpleContent SET userName = @newName, infoContent = @newIntro, educContent = @newEduc, workContent = @newWork WHERE userID = @userName", connection);
        try
        {
            string username = HttpContext.Current.User.Identity.Name;
            myCommand.Parameters.AddWithValue("@userName", username.ToString());
            myCommand.Parameters.AddWithValue("@newName", newName.ToString());
            myCommand.Parameters.AddWithValue("@newIntro", newIntro.ToString());
            myCommand.Parameters.AddWithValue("@newEduc", newEduc.ToString());
            myCommand.Parameters.AddWithValue("@newWork", newWork.ToString());
            myCommand.ExecuteNonQuery();
            connection.Close();
        }
        catch
        {
            Response.Redirect("http://www.google.co.uk");
        }

    }
}

我将不胜感激您可能有的任何指示。

使用 ContentEditable 通过 ASP.NET 保存到数据库中

尝试将代码格式化:

protected void saveBtn_Click(object sender, EventArgs e)
{    
// add variables
     string connectionInfo = (...)
     string commandText = (...)
    using (...){
        SqlCommand myCommand = (...)
        // add parameters
    try
    {
        connection.Open();
        myCommand.ExecuteNonQuery();
        connection.Close();
    }
    catch (Exception ex)
            {
                (...)
            }
}