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
Short answer is I guess you only need to update your controls accordingly when there is data.
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
andlblPageStatus.Text
could be set in one place with single line of code(but a bit more complicated readibility).Things to consider:
Make your code as much readable as it is possible(means anyone could read your code)