Prakash Kumar Prakash Kumar - 4 months ago 15
HTML Question

date availability checker working only once

The problem I have here is, that the code actually works, but it works only once.
For example if I have selected a date and time thats already in the database it shows date unavailable as its supposed to and if I select a date thats not in db it shows date available. No problem until then, but after these both labels have been shown once and if i select an unavailable date the third time, the code doesnt execute again. I mean, it just stays at the previous shown label. How can I make a loop on this?

The front end code is as below:

<form id="form1" runat="server">
<asp:ScriptManager ID="scriptmanager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript" language="javascript">
Sys.Application.add_load(jScript);
</script>
<div id="dvRecWed" style="display: none">
<asp:UpdatePanel ID="PnlUsrDetails" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtReceptionDate" class="form-control" autocomplete="off" CssClass="datepicker" placeholder="mm/dd/yy" AutoPostBack="true" OnTextChanged="reset_ddl" runat="server"></asp:TextBox>
<asp:DropDownList AutoPostBack="true" ID="ddlReceptionTime" runat="server" OnSelectedIndexChanged="txtUsername_TextChanged">
<asp:ListItem Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="Forenoon">Forenoon</asp:ListItem>
<asp:ListItem Value="Afternoon">Afternoon</asp:ListItem>
<asp:ListItem Value="FullDay">Full Day</asp:ListItem>
</asp:DropDownList>
<div id="checkdate" class="checkdate" runat="server" visible="false">
<asp:Image ID="imgstatus" runat="server" Width="17px" Height="17px" />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>


And the back end code is as below

protected void txtUsername_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ddlReceptionTime.Text))
{
string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
OleDbConnection connection = new OleDbConnection(connString);
// SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName=@Name", con);
string selectQuery = "SELECT FunctionTime FROM function WHERE FunctionDate=@FunctionDate AND FunctionTime='FullDay'";
connection.Open();
OleDbCommand command = new OleDbCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@FunctionDate", txtReceptionDate.Text);
command.Parameters.AddWithValue("@FunctionTime", ddlReceptionTime.Text);
OleDbDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/cross.png";
lblStatus.Text = "Date Unavailable";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
else
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/check.png";
lblStatus.Text = "Date available";
}
}

else if (!string.IsNullOrEmpty(ddlReceptionTime.Text))
{
string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
OleDbConnection connection = new OleDbConnection(connString);
// SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName=@Name", con);
string selectQuery = "SELECT FunctionTime FROM function WHERE FunctionDate=@FunctionDate AND FunctionTime=@FunctionTime";
connection.Open();
OleDbCommand command = new OleDbCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@FunctionDate", txtReceptionDate.Text);
command.Parameters.AddWithValue("@FunctionTime", ddlReceptionTime.Text);
OleDbDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/cross.png";
lblStatus.Text = "Date Unavailable";
lblStatus.ForeColor= System.Drawing.Color.Red;

}
else
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/check.png";
lblStatus.Text = "Date Available";
}
}
else
{
checkdate.Visible = false;
}
}



protected void reset_ddl(object sender, EventArgs e)
{
ddlReceptionTime.SelectedValue = "-1";
}

Answer

What is your expected behavior?

<asp:TextBox ID="txtReceptionDate" class="form-control" autocomplete="off" CssClass="datepicker" placeholder="mm/dd/yy" AutoPostBack="true" OnTextChanged="reset_ddl" runat="server"></asp:TextBox>
<asp:DropDownList AutoPostBack="true" ID="ddlReceptionTime" runat="server" OnSelectedIndexChanged="txtUsername_TextChanged">
    <asp:ListItem Value="-1">--Select--</asp:ListItem>
    <asp:ListItem Value="Forenoon">Forenoon</asp:ListItem>
    <asp:ListItem Value="Afternoon">Afternoon</asp:ListItem>
    <asp:ListItem Value="FullDay">Full Day</asp:ListItem>
</asp:DropDownList>

Based on your code you are calling the txtUsername_TextChanged during the OnSelectedIndexChanged event of the drop down list.

This means even if you change the date value of your date picker, the event won't fire. Instead, reset_ddl will trigger during the OnTextChanged event.

We don't know what does reset_dll do. If you want the code that checks the availability of the date to trigger then place that on the OnTextChanged event of the date picker textbox and not on the drop down list.