I have on Textbox, where I load text from db then edit and save, but when I set TextBox2.Text in BindTextBox, and I finish edit it, it has same value in InsertToDatabase like in BindTexBox method it does not metter i edited it or not.
protected void Page_Load(object sender, EventArgs e)
{
BindTextBox
}
protected void BindTextBox()
{
SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);
SqlCommand myCommand = new SqlCommand("dbo.Search", myConnection);
string message = "";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@ID", SqlDbType.NVarChar, 20).Value = Utility.qID;
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
message = reader["Description"].ToString();
}
myConnection.Close();
TextBox2.Text = message;
}
protected void InsertToDatabse()
{
string text = TextBox2.Text.ToString();
SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);
SqlCommand myCommand = new SqlCommand("dbo.Insert", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Clear();
myCommand.Parameters.Add("@ID", SqlDbType.NVarChar, 20).Value = Utility.qID;
myCommand.Parameters.Add("@Description", SqlDbType.NVarChar, 200).Value = text;
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
InsertToDatabse();
BindTextBox();
}
You should check for if your page is postback
When you press your save button a postback to server is done and your load method is called again. This causes your textbox value to overridden since you call BindTextBox on both initial load and postbacks.