This is because your "count" value isn't defined within the scope of the SqlDataSource1_Selected method. In order to access it in this way, you would likely want to define it as either a static or page/class level variable as seen below :
public partial class admin_UpdateMember : System.Web.UI.Page { public static int NumberOfRecords = 0; protected void Page_Load(object sender, EventArgs e) { } protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e) { TextBox1.Text = NumberOfRecords.ToString(); } protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e) { // Omitted for brevity // Set the count NumberOfRecords = (Int32)myCommand.ExecuteScalar(); // Omitted for brevity } }
Or you could simply set the TextBox value within the OnRowCommand method itself :
// Get the count var count = (Int32) myCommand.ExecuteScalar(); // Set the TextBox value TextBox1.Text = count.ToString();