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>
<asp:Label ID="dash" runat="server" Text="To"></asp:Label>
<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>
<asp:Label ID="dash1" runat="server" Text="To"></asp:Label>
<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);
}
}