I ran into an issue trying to find a textbox control on the same page. It keeps giving me a null when I debug it.
Here is my code:
public void UpdateTimeLog(string input)
{
string timeNumber = "txtTime" + input;
TextBox myTextbox = (TextBox)FindControl(timeNumber);
sqlConnection.Open();
using (var command = new SqlCommand("UPDATE [JobSheet] SET [Time" + input + "]=@Time" + input + " WHERE [JobShtId]=@JobShtId", sqlConnection))
{
command.Parameters.AddWithValue("@JobShtId", jobSheetId);
command.Parameters.AddWithValue("@Time" + input + "", myTextbox.Text);
command.ExecuteNonQuery();
}
sqlConnection.Close();
}
Problem Solved: I didn't specify that the textbox control was in a container(e.g. ContentPlaceHolder1).
Here is the corrected code:
public void UpdateTimeLog(string input)
{
var container = Master.FindControl("ContentPlaceHolder1");
string timeNumber = "txtTime" + input;
TextBox myTextbox = (TextBox)container.FindControl(timeNumber);
sqlConnection.Open();
using (var command = new SqlCommand("UPDATE [JobSheet] SET [Time" + input + "]=@Time" + input + " WHERE [JobShtId]=@JobShtId", sqlConnection))
{
command.Parameters.AddWithValue("@JobShtId", jobSheetId);
command.Parameters.AddWithValue("@Time" + input + "", myTextbox.Text);
command.ExecuteNonQuery();
}
sqlConnection.Close();
}