How do I hide some labels if the value for datetime DataType are default

Donald Symmons 2,861 Reputation points
2023-05-08T08:19:31.5366667+00:00

How do I hide labels that display unwanted values if I don’t want to?

I have a form with TextBoxes where user inserts their values into database. Four of the Textboxes are to input Dates and Times.

Example, 1.DateFrom – 2. DateTo and 3. Time From – 4. Time To

The date Textboxes have their TextMode set to “date” and the Textboxes for time set to “Time”. Then in my Table, I set the Data Type for each Date and Time data to “datetime”. The process is okay and Data is inserted into the table.

But where I have issue is that I can’t hide some labels that have DateTo and TimeTo values when their values are default. For instance, when a user registers a seminar, the user inserts the date and Time from when the seminar will start and end. If the seminar will be only for one day, then the user will not input any value into DateTo and TimeTo Textboxes; the DateTo and TimeTo Textboxes will be empty. But if the seminar will be for 3 days, then the user will input value into DateTo and TimeTo Texboxes.

Now for one day seminar, when user inserts data into table, leaving the DateTo and TimeTo textboxes empty, regardless of these data will be still be inserted by default into those columns because I set the DataType of each date and time columns to datetime in the table.

Here is how the Table looks like. In this below table, the user with ID = 1 did not insert DateTo and TimeTo values.

ID = 2 inserted into DateTo and TimeTo

This is my Table and the data inserted. some dates and time were inserted by default, because the textboxes were left empty when the data inserted.

user 1 did not insert input any data into DateTo and TimeTo columns

Id DateFrom TimeFrom DateTo TimeTo
1 24-May-23 12:00:00 AM 01-Jan-00 11:30:00 AM 01-Jan-00 12:00:00 AM 01-Jan-00 12:00:00 AM
2 29-May-23 12:00:00 AM 01-Jan-00 9:00:00 AM 31-May-23 12:00:00 AM 01-Jan-00 9:00:00 AM

 I just want to hide the labels that show DateTo and TimeTo if the values are default values. If the user did not insert into those columns (i.e., the event is a one-day event). It should only show DateFrom and TimeFrom. Then if the event has another Date and Time, it should show the DateTo and TimeTo labels

<div class="col-md-12">
  <i class="fal fa-calendar-alt" aria-hidden="true" style="margin: 0 auto; font-size: 10pt; color: #0b2436;"></i>
  <asp:Label ID="datefrom" runat="server" Text=""></asp:Label>&nbsp;&nbsp;
  <asp:Label ID="dash" runat="server" Text="To"></asp:Label>&nbsp;&nbsp;
  <asp:Label ID="dateto" runat="server" Text=""></asp:Label>
</div>
<div class="col-md-12">
  <i class="fal fa-clock" aria-hidden="true" style="margin: 0 auto; font-size: 10pt; color: #0b2436;"></i>
  <asp:Label ID="timefrom" runat="server" Text=""></asp:Label>&nbsp;&nbsp;
  <asp:Label ID="dash1" runat="server" Text="To"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <asp:Label ID="timeto" runat="server" Text=""></asp:Label>
</div>
protected void Page_Load(object sender, EventArgs e)
        {
           EventregData();
        }
        private void EventregData()
        {
            try
            {
                if (Session["eventID"] != null)
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT * FROM TblEvnt WHERE eventName =@eventName AND Id =@Id", con))
                        {
                            cmd.Parameters.AddWithValue("@eventName", Session["eventID"]);
                            cmd.Parameters.AddWithValue("@Id", Session["Id"]);
                            con.Open();
                            SqlDataReader dr = cmd.ExecuteReader();
                            if (dr.Read())
                            {
                                datefrom.Text = DateTime.Parse(dr[8] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[8]).ToString()).ToString("MMM d, yyyy");
                                timefrom.Text = DateTime.Parse(dr[9] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[9]).ToString()).ToString("hh:mm tt");
 
                                dateto.Text = DateTime.Parse(dr[10] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[10]).ToString()).ToString("MMM d, yyyy");
                                timeto.Text = DateTime.Parse(dr[11] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[11]).ToString()).ToString("hh:mm tt");
                                 
                                //This is how I tried to hide the labels of DateTo and TimeTo if the event is a One-day event
                                string longdate = dr[10].ToString().ToString();
                                string longtime = dr[11].ToString().ToString();
                                if (longdate != null && longtime != null)
                                {
                                    dash.Visible = true;
                                    dash1.Visible = true;
                                    dateto.Text = DateTime.Parse(dr[10] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[10]).ToString()).ToString("MMM d, yyyy");
                                    timeto.Text = DateTime.Parse(dr[11] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[11]).ToString()).ToString("hh:mm tt");
                                }
                                else
                                {
                                    dash.Visible = false;//I also try to hide the labels that are used "To" value
                                    dash1.Visible = false;
                                    dateto.Visible= false;
                                    timeto.Visible = false;
                                }
                            }
                        }
                    }
                }
                else
                {
                    Response.Redirect("https://localhost:44316/EventRegPage");
                }
            }
            catch (SqlException ex)
            {
                string msg = "Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
        }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. QiYou-MSFT 4,311 Reputation points Microsoft Vendor
    2023-05-10T07:05:23.7533333+00:00

    Hi @Donald Symmons

    From your code, you mean that if "timefrom" and "timeto" are on the same day, "dash" and "dash1" are hidden.

    In your code you are comparing to "null". But in reality it has a default value and you don't know it. So my idea is to directly modify the judgment conditions here and judge them to perform hidden operations in one day.

    As I understand it, you mean the same day, omitting the two 'To' in "dash" and "dash1".

    string datefrom = dr[8].ToString().ToString();
    string dateto = dr[10].ToString().ToString();
    if (datefrom==dateto)
    {
    dash.Visible = true;
    dash1.Visible = true;
    dateto.Text = DateTime.Parse(dr[10] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[10]).ToString()).ToString("MMM d, yyyy");
    timeto.Text = DateTime.Parse(dr[11] == DBNull.Value ? (string)null : Convert.ToDateTime(dr[11]).ToString()).ToString("hh:mm tt");
    }
    

    TIME2

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Albert Kallal 5,231 Reputation points
    2023-05-09T23:49:43.44+00:00

    Hum, if there are no values, and they are null?

    Then using say a gridview, those columns will not show any value.

    So, say this markup:

        <asp:GridView ID="GridView1" runat="server" GridLines="none"
            AutoGenerateColumns="False" cssclass="table table-hover"
            DataKeyNames="ID" width="30%">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="DateFrom" HeaderText="Date From" DataFormatString="{0:D}" />
                <asp:BoundField DataField="TimeFrom" HeaderText="TimeFrom" DataFormatString="{0:hh:mm tt}"  />
                <asp:BoundField DataField="DateTo" HeaderText="DateTo" DataFormatString="{0:D}" />
                <asp:BoundField DataField="TimeTo" HeaderText="TimeTo" DataFormatString="{0:hh:mm tt}"  />
            </Columns>
        </asp:GridView>
    
    

    And code to load is this:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                LoadGrid
            End If
    
        End Sub
    
    
        Sub LoadGrid()
    
            Dim strSQL =
                "SELECT * FROM tblEvents ORDER BY DateFrom, TimeFrom"
    
            GridView1.DataSource = MyRst(strSQL)
            GridView1.DataBind()
    
        End Sub
    
    

    And the result is now this:

    gr

    So, if those columns are null, then they don't display at all when using a grid view.

    So, consider a gridview, or even a listview. As above shows, I formatted the data as a full date, but I could specify a 2023-09-03, or whatever for the format.

    And depending on your data source, as noted, if the values are "null" and not entered for those columns, as a general rule, the values hide all by themselves anyway.