single-use date availability checker

I'm currently facing an issue with my code. It seems to be functioning correctly, but only for the first iteration. Let me explain further.

If I select a date and time that is already in the database, it correctly displays "date unavailable". Likewise, if I select a date that is not in the database, it shows "date available". So far, so good.

However, after these labels have been displayed once, if I select an unavailable date for the third time, the code doesn't execute again. It simply remains on the previously shown label. Is there a way to create a loop for this?

The front end code:

<form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanager1" runat="server">
    </asp:ScriptManager>
    <script type="text/javascript" language="javascript">
        Sys.Application.add_load(jJavaScript);
    </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>

Back end code:

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 №1

What behavior are you expecting?

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

Based on the given code, the txtUsername_TextChanged method is called when the drop-down list's OnSelectedIndexChanged event occurs.

This implies that changing the date value in your date picker will not trigger any events. Instead, the refresh_ddl method will be triggered during the OnTextChanged event.

We are unsure of what functionality is present in the refresh_ddl method. If you want to check the availability of the chosen date, it should be placed within the OnTextChanged event of the date picker textbox and not in the drop-down list.

Answer №2

After a thorough investigation, I successfully identified the root cause of the issue. Surprisingly, the code is flawless and functioning perfectly. However, upon inspecting my database, I discovered the underlying problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for preventing errors in formatting?

I created a field using rectangles as divs. However, when I add content to the rectangle divs, the formatting gets messed up. Can anyone help me fix this issue? Here's the link <div class='line' id='line1'> <div class=& ...

Writing and altering content within the <code> element in Chrome is unreliable

Utilizing a WYSIWYG Editor with contenteditable functionality allows users to input "code snippets" using a <code> element. Here is an example: <div contenteditable="true"> <p> This is a paragraph with an <code>inline s ...

Images for the background of the table header and sorting icons

Currently, I am utilizing jquery.tablesorter for table sorting functionality. However, I have a desire to apply a pattern to my table headers using a background-image. Unfortunately, when I do so, the .headerSortUp and .headerSortDown classes on the sorted ...

Tips for resolving the issue of cypress automatically opening a new tab

Recently, I've encountered an issue while using Cypress to perform a test on my website. Every time I click on a button, it redirects me to a new tab, disrupting the flow of my test with Cypress. Despite trying to remove the "target" attribute using t ...

Interacting between frames with jQuery

I have main_page.htm with the following frameset structure: <frameset rows="30,*" frameborder=0 border=0> <frame name="top_frame" src="top.htm"> <frame name="bottom_frame" src="bottom.htm"> </frameset> The content in ...

The search box and submit button display without any visible borders

Question: I am facing an issue with the appearance of the searchbox and submit button in my PHP project. They look different compared to a simple HTML form. How can I fix this? The code on the PHP subpage is as follows: <header id="header"> <div ...

The Art of Validating Forms in Vue.js

Currently I am in the process of developing a form with validation using Vue, however, I've run into some errors that are showing up as not defined even though they are currently defined. HTML <form class="add-comment custom-form" @submit="checkF ...

What is the proper way to generate an iframe with a width set to "100%" or left empty, rather than width = "100"?

I am currently utilizing vimeowrap to iterate through a playlist of videos. I would like the iframe that is generated by vimeowrap to have either a width and height set to "100%" or nothing at all. For more information on Vimeo Wrap, visit: To see my tes ...

Changing the size of an image in an HTML5 canvas

I have been attempting to generate a thumbnail image on the client side using Javascript and a canvas element. However, when I reduce the size of the image, it appears distorted. It seems as though the resizing is being done with 'Nearest Neighbor&apo ...

Resizing the elements within an iframe: Troubleshooting problems with embedding Bandcamp players

My goal is to embed a Bandcamp music player on my WordPress blog page, but I'm facing a challenge. The maximum width of the current player is 700px and I need it to be 900px. After consulting with someone at Bandcamp, they directed me to the old versi ...

Can you suggest the best HTML5 structure for a multi-chapter novel?

In the process of creating a small HTML5 book with chapters and sections, I encountered a dilemma when it comes to structuring my documents like this: chapter1.html - introduction to chapter 1 chapter1section1.html - section 1.1 chapter1section2.ht ...

Design your HTML select element with a unique custom arrow where the text elegantly appears over the arrow

I've encountered an issue with a select element in my HTML page that has a custom arrow. The problem is that the text within the select element overlaps with the arrow, which is not desired. I'm seeking a cross-browser solution for this specific ...

Obtaining the referring URL after being redirected from one webpage to another

I have multiple pages redirecting to dev.php using a PHP header. I am curious about the source of the redirection. <?php header(Location: dev.php); ?> I attempted to use <?php print "You entered using a link on ".$_SERVER["HTTP_REFERER"]; ?> ...

Open the overflow div within a table data cell

My goal is to create a drag-and-drop schedule tool similar to Fullcalendar. I have decided to use a table layout with a rectangular div inside each TD cell to represent tasks. <table onDragEnd={dragend}> <tr> <th>0</th> ...

Using React.js to create table cells with varying background colors

For my React application, I am working with a table that utilizes semantic ui. My goal is to modify the bgcolor based on a condition. In most cases, examples demonstrate something like bgcolor={(condition)?'red':'blue'}. However, I requ ...

Verify the position of the scrollbar without triggering any reflow

Here is a function I have: is_bottom: function() { if (settings.scrollBottomOffset === -1) { return false; } else { var scroll_height, scroll_top, height; scroll_height = ...

Is there a way to have a fixed width div positioned in the center of the screen, with two additional divs on either side

I have come across this stunning image: https://i.stack.imgur.com/tUYMc.png It serves as a header for a website - click on it to see the full size. I am trying to replicate this using HTML, CSS, and images, but I'm struggling. It needs to be 100% w ...

Deactivate click events in the container div

Here is the html code snippet that I am working with: <div class="parent" ng-click="ParentClick()"> . . . <div class="child" ng-click="ChildClick()"> Some Text </div> </div> When clicking on Som ...

Cross-origin resource sharing problem arises when JavaScript is loaded asynchronously using script tags created dynamically

By dynamically creating a script as shown below, the JavaScript source is downloaded asynchronously. let newScript = document.createElement('script'); newScript.src = srcUrl; let firstScript = document.getElementsByTagName('script')[0] ...

Opacity of absolutely positioned elements

I am currently working on creating a popup box that will gray out the surrounding area. The problem I'm facing is that the opacity of the shadow div seems to be overriding that of the popup. I have tried changing the position from absolute to fixed an ...