SMica SMica - 3 years ago 143
ASP.NET (C#) Question

How can I calculate the NoOfDays label correctly that will be displayed on the Confirm aspx page?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace XEx08Reservation
{
public partial class Request : System.Web.UI.Page
{
private string currentDate = DateTime.Today.ToShortDateString();
private string currentYear = DateTime.Today.Year.ToString();


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// set copyright year no matter what
lblYear.Text = currentYear;

// display saved reservation data if there is any
if (Session["Reservation"] != null)
{
DisplayReservation();
}
else {
// otherwise, set default values
txtArrivalDate.Text = currentDate;
rdoKing.Checked = true;
}
}
}

private void DisplayReservation()
{
Reservation reservation = (Reservation)Session["Reservation"];

txtArrivalDate.Text = reservation.ArrivalDate.ToShortDateString();
txtDepartureDate.Text = reservation.DepartureDate.ToShortDateString();
ddlNoOfPeople.SelectedValue = reservation.NoOfPeople.ToString();

if (reservation.BedType == "King")
rdoKing.Checked = true;
else if (reservation.BedType == "Two Queens")
rdoDouble.Checked = true;
else
rdoSingle.Checked = true;

txtSpecialRequests.Text = reservation.SpecialRequests;
txtFirstName.Text = reservation.FirstName;
txtLastName.Text = reservation.LastName;
txtEmail.Text = reservation.Email;
txtPhone.Text = reservation.Phone;
ddlPreferredMethod.SelectedValue = reservation.PreferredMethod;
}

protected void btnClear_Click(object sender, EventArgs e)
{
txtArrivalDate.Text = currentDate;
txtDepartureDate.Text = "";
ddlNoOfPeople.SelectedIndex = -1;
rdoKing.Checked = true;
rdoDouble.Checked = false;
rdoSingle.Checked = false;
txtSpecialRequests.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
txtEmail.Text = "";
txtPhone.Text = "";
ddlPreferredMethod.SelectedIndex = 0;
lblMessage.Text = "";
}

protected void btnSubmit_Click(object sender, EventArgs e)

{
Reservation reservation = new Reservation();
reservation.ArrivalDate = Convert.ToDateTime(txtArrivalDate.Text);
reservation.DepartureDate = Convert.ToDateTime(txtDepartureDate.Text);
TimeSpan tsDuration = reservation.DepartureDate - reservation.ArrivalDate;
reservation.NoOfDays = Convert.ToInt32(tsDuration);
tsDuration.Days.ToString();
reservation.NoOfPeople = Convert.ToInt32(ddlNoOfPeople.SelectedValue);
reservation.PreferredMethod = Convert.ToString(ddlPreferredMethod.SelectedValue);

if (rdoKing.Checked) reservation.BedType = "King";
else reservation.BedType = "Double Double";

reservation.SpecialRequests = txtSpecialRequests.Text;
reservation.FirstName = txtFirstName.Text;
reservation.LastName = txtLastName.Text;
reservation.Email = txtEmail.Text;
reservation.Phone = txtPhone.Text;

Session["Reservation"] = reservation;
Response.Redirect("Confirm.aspx");

}
}
}


Hello Everyone,

I have web application in which I am initializing a new reservation object and then set it properties based on the values the user enters on the form; however, I am having trouble correctly calculating the total numbers of days between the
ArrivalDate
and the
DepartureDate
that will eventually be displayed in a label on the
Confirm.aspx
page. Can someone help me?

Answer Source

Use Subtract to find the diff and use the days property to get the exact number of days.

var ArrivalDate = DateTime.Parse( "09 Aug 2008");
var DepartureDate = DateTime.Parse("03 Aug 2009");
var days = DepartureDate.Subtract(ArrivalDate).Days;

I over looked your code. you are actually doing close to the above. Please let me know what is the exact issue. It looks like it is not just the date calculation. More like conversion from datetime from string. If this is what you are facing, Use the below suggesions

  1. Follow the current culture while getting user inputs
  2. Use the same culture to convert date from the string
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download