Unable to hide the panel

68 views Asked by At

I am making an asp.net Web Forms Application. If any user gives wrong input, then it shows validation. But, the second time when the user gives correct input, the code works properly, only problem is the error message does not get removed.

In the Page Load Function

pnlStatus.Visible = true;

The Button_Click Event is written Below

 protected void btnAffRntGoClick(object sender, EventArgs e)
{
    DateTime dtFrom = new DateTime();
    DateTime dtTo = new DateTime();
    bool dt1 = true;
    bool dt2 = true;
    int result;

    if (DateTime.TryParse(txtStartDate.Text, out dtFrom) == false)
    {
        dtFrom = new DateTime(1900, 1, 1);
        dt1 = false;
    }

    if (DateTime.TryParse(txtEndDate.Text, out dtTo) == false)
    {
        dtTo = new DateTime(2100, 1, 1);
        dt2 = false;
    }
    result = DateTime.Compare(dtTo, dtFrom);
    if ((dt1 == true && dt2 == true) && result > 0)
    {
        lblPageStatus.Text = string.Empty;
        pnlStatus.Visible = false;


        DataSet ds = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(), "SPNAME",
        Convert.ToInt64(ddl.SelectedValue), dtFrom.Date.ToShortDateString(), dtTo.Date.ToShortDateString(), ddl2.SelectedValue, Chk1.Checked);

        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
// Code to generate Excel
        }
        else
        {
            pnlStatus.Visible = true;
            lblPageStatus.Text = "No data available to export.";
        }
    }
    else
    {
        pnlStatus.Visible = true;
        lblPageStatus.Text = "Please Check the Dates";
    }
}

Anytype of help will be appreciated.

Thanks in Advance

1

There are 1 answers

0
a-man On

Short answer is I guess you only need to update your controls accordingly when there is data.

    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
    {
       // Code to generate Excel
        pnlStatus.Visible = false;
        lblPageStatus.Text = string.Empty;
    }
    else
    {
        pnlStatus.Visible = true;
        lblPageStatus.Text = "No data available to export.";
    }

Long answer - your code should be cleaned and alligned with some standards to achieve readibility. Probably this is the reason you got lost. Additional methods could be introduced for simplifying. Below is quick overview. Additionally pnlStatus.Visible and lblPageStatus.Text could be set in one place with single line of code(but a bit more complicated readibility).

Things to consider:

  • Give meaningfull names to your variables
  • Avoid code duplication(follow DRY,KISS,YAGNI; use extension methods)
  • Make your code as much readable as it is possible(means anyone could read your code)

    private const string connectionString = 
    ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    
    protected void btnAffRntGoClick(object sender, EventArgs e)
    {
        var from = new DateTime(1900, 1, 1);
        var to = new DateTime(2100, 1, 1);
    
        var isValidStartDate = !DateTime.TryParse(txtStartDate.Text, out from);
        var isValidEndDate = !DateTime.TryParse(txtEndDate.Text, out to);
    
        var hasValidDates = isValidStartDate && isValidEndDate;
        var hasDifference = DateTime.Compare(from, to) > 0;
    
        var shouldCheckDates = !hasValidDates || !hasDifference;
    
        if (shouldCheckDates)
        {
            pnlStatus.Visible = true;
            lblPageStatus.Text = "Please Check the Dates";
            return;
        }
    
        DataSet ds = SqlHelper.ExecuteDataset(connectionString,
            "SPNAME",
            Convert.ToInt64(ddl.SelectedValue)
            , @from.Date.ToShortDateString(),
            to.Date.ToShortDateString()
            , ddl2.SelectedValue
            , Chk1.Checked
        );
    
        var hasData = ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0;
    
        pnlStatus.Visible = !hasData;
        lblPageStatus.Text = hasData ? string.Empty : "No data available to export.";
    
    }