user1954418
user1954418

Reputation: 1013

Clear value of label

I have this code called when a button is pressed:

protected void Button1_Click(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header")
    {
        using (SqlConnection con = 
                   new SqlConnection(ConfigurationSettings.AppSettings["DBcon"]))
        {
            if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
            {
                lbl_NoBatchID.Text = "Please enter BatchID!";
            }
            else
            {
                try
                {
                    SqlCommand sqlCommand = new SqlCommand(
                        "Select * from tbl_WinApps_FileHeader Where BatchID =" +
                        TextBox_ID.Text.ToString());

                    sqlCommand.Connection = con;
                    con.Open();
                    SqlDataReader read = sqlCommand.ExecuteReader();

                    GridView1.DataSource = read;
                    GridView1.DataBind();
                }
                catch (Exception)
                {                           
                }
            }
        }
    }
}

This is how it works: The user inputs a ID, then when the button is pressed, It will display the table in SQL.

If the user did not input anything on the textbox, it prompts "Please enter BatchID!" but after that, it stays in there and it doesn't clear even if I already entered a valid ID. Any idea why that is?

Upvotes: 0

Views: 33193

Answers (3)

Ankur Singhal
Ankur Singhal

Reputation: 71

In your Else block add this line of code.

lbl_NoBatchID.Text = "";

Upvotes: 0

NET Experts
NET Experts

Reputation: 1536

   protected void Button1_Click(object sender, EventArgs e)
{
   if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header")
    {

        using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["DBcon"]))
        {
            if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
            {
                lbl_NoBatchID.Text = "Please enter BatchID!";

            }
            else
            {
                try
                {

                    SqlCommand sqlCommand = new SqlCommand("Select * from tbl_WinApps_FileHeader Where BatchID =" + TextBox_ID.Text.ToString());
                    sqlCommand.Connection = con;
                    con.Open();
                    SqlDataReader read = sqlCommand.ExecuteReader();

                        GridView1.DataSource = read;
                        GridView1.DataBind();
                    lbl_NoBatchID.Text = "";
                }
                catch (Exception)
                {                           

                }

            }
        }

    }

Upvotes: 1

Eric Falsken
Eric Falsken

Reputation: 4932

ASP.NET pages have something called ViewState that can maintain the state of the controls between requests. You need to clear the DataSource value of the GridView and rebind it.

           if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
            {
                lbl_NoBatchID.Text = "Please enter BatchID!";
                GridView1.DataSource=null;
                GridView1.DataBind();
            }
            else
            {

You also want to clear the error if the lookup was successful:

catch(Exception e){
}
lbl_NoBatchID.Text = "";

I should also note that your empty catch() will swallow any database or lookup errors you might get.

Upvotes: 1

Related Questions