asp.netgridviewtimecomparevalidator

How to validate time using compare validator?


How to validate time using compare validator?

I have a Editable gridview that contain 2 column for time Start & End Time and I want to validate them that star time must be earlier than end time. I have 24 hour format.

For Example If I entered Start Time: 20:30 and the End Time is 19:30 it should give me error and prevent me from Updating the row


Solution

  • simply you can try this

    In aspx code

    <div>
        <asp:GridView ID="gvDetails" DataKeyNames="EventID,EventName" runat="server" AutoGenerateColumns="false"
            CssClass="Gridview" HeaderStyle-BackColor="#61A6F8" ShowFooter="true" HeaderStyle-Font-Bold="true"
            HeaderStyle-ForeColor="White" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
            OnRowDeleting="gvDetails_RowDeleting" OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating"
            OnRowCommand="gvDetails_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
                            ToolTip="Update" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
                            ToolTip="Cancel" Height="20px" Width="20px" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg"
                            ToolTip="Edit" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server"
                            ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg"
                            CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EventName">
                    <EditItemTemplate>
                        <asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("EventName") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("EventName") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrusrname" runat="server" />
                        <asp:RequiredFieldValidator ID="rfvEventName" runat="server" ControlToValidate="txtftrusrname"
                            Text="*" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StartTime">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrStartTime" runat="server" />
                        <asp:RequiredFieldValidator ID="rfvStartTime" runat="server" ControlToValidate="txtftrStartTime"
                            Text="*" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EndTime">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtstate" runat="server" Text='<%#Eval("EndTime") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblstate" runat="server" Text='<%#Eval("EndTime") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrEndTime" runat="server" />
                        <asp:RequiredFieldValidator ID="rfvEndTime" runat="server" ControlToValidate="txtftrEndTime"
                            Text="*" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    

    And at code behind

    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName.Equals("AddNew"))
                {
                    TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
                    TextBox txtStartTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrStartTime");
                    TextBox txtEndTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrEndTime");
                    if (Convert.ToDateTime(txtEndTime.Text.Trim().ToString()) <= Convert.ToDateTime(txtStartTime.Text.Trim().ToString()))
                    {
                        lblresult.Text = "Please enter valid end time";
                    }
                    else
                    {
                        con.Open();
                        SqlCommand cmd =
                            new SqlCommand(
                                "insert into tblEvents(EventName,StartTime,EndTime) values('" + txtUsrname.Text + "','" +
                                txtStartTime.Text + "','" + txtEndTime.Text + "')", con);
                        int result = cmd.ExecuteNonQuery();
                        con.Close();
                        if (result == 1)
                        {
                            BindEmployeeDetails();
                            lblresult.ForeColor = Color.Green;
                            lblresult.Text = txtUsrname.Text + " Details inserted successfully";
                        }
                        else
                        {
                            lblresult.ForeColor = Color.Red;
                            lblresult.Text = txtUsrname.Text + " Details not inserted";
                        }
    
    
                    }
    
                }
            }
    

    enter image description here