почему не удается обновить данные в форме?

#asp.net

#asp.net

Вопрос:

В следующем коде после повторного нажатия обновите информацию в текстовом поле и не измените информацию в базе данных. Пожалуйста, направьте меня.

     protected void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["tel"].ConnectionString;

        SqlConnection telConnection = new SqlConnection(ConnectionString);
        string strSelect = "SELECT * FROM telephon WHERE  ID= @ID ";
        SqlCommand telCommand = new SqlCommand(strSelect,telConnection);
        telCommand.Parameters.AddWithValue("@ID",Request.QueryString["Code"]);
        telConnection.Open();
        SqlDataReader dr = telCommand.ExecuteReader();
        dr.Read();
        txtCode.Text = dr["ID"].ToString();
        txtName.Text = dr["telName"].ToString();
        txtFamily.Text = dr["telFamily"].ToString();
        txtOrgan.Text = dr["telOrgan"].ToString();
        txtTel1.Text = dr["telTel1"].ToString();
        txtTel2.Text = dr["telTel2"].ToString();
        txtMob1.Text = dr["telMob1"].ToString();
        txtFax.Text = dr["telFax"].ToString();
        dr.Close();
        telConnection.Close();           
    }

    protected void btnReg_Click(object sender, EventArgs e)
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["tel"].ConnectionString;
        SqlConnection telConnection = new SqlConnection(ConnectionString);
        SqlCommand telCommand = new SqlCommand();
        telCommand.Connection = telConnection;
        telConnection.Open();
        telCommand.CommandText = "UPDATE telephon SET [telName]='"   txtName.Text   "' , [telFamily]='"   txtFamily.Text  
            "',[telOrgan]='"   txtOrgan.Text   "' ,[telTel1]='"   txtTel1.Text   "' ,[telTel2]='"   txtTel2.Text  
            "',[telMob1]='"   txtMob1.Text   "', [telFax]='"   txtFax.Text   "' WHERE ID="   Convert.ToInt32(txtCode.Text.Trim())   "";
        telCommand.ExecuteNonQuery();
        telConnection.Close();
        Response.Redirect("Index.aspx");
    }
  

Комментарии:

1. Я не вижу фиксации… Если вы не зафиксируете, изменения не будут обновлены.. Я не знаком с ASP.NET но я бы подозревал, что это не произойдет автоматически.

Ответ №1:

Вы не зафиксировали транзакцию см. Этот пример, у вас должен быть:

    telCommand.Commit();
  

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.commit (v = против 110).aspx