我正在尝试消除我的 Web 应用程序中的错误。基本上我正在尝试实施餐厅预订系统。用户选择他们的聚会中有多少人,坐下(午餐或晚餐)和日期。然后系统查询我的数据库,让用户知道是否有可用的数据库。如果餐厅已经有人预订,但如果查询返回 NULL 值(即餐厅为空),则系统崩溃。我以前从来没有像这样处理过 NULL,我不知道如何处理代码中的这个错误,以便用户可以进行预订。我已经尽可能地评论了我的代码。任何帮助是极大的赞赏!:)
protected void AvailabilityButton_Click(object sender, EventArgs e)
{
//Create SQL Database connection
// New sql connection and command
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = "Data Source=STUDENT2;Initial Catalog=HarryBistro;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
myconn.Open();
//Check that selected date is today or later
if (Calendar1.SelectedDate <= DateTime.Today)
{
SuccessFailureLabel.Text = "Please select a date in the future";
SuccessFailureLabel.Visible = true;
}
else
{
//Create variables from user input
string SelectedBranch = BranchDropDownList.SelectedValue.ToString();
string SelectedSitting = SittingDropDownList.SelectedValue.ToString();
int SelectedDiners = Convert.ToInt32(DinersDropDownList.SelectedValue);
string SelectedDate = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
//Query to find out how many people have already booked into selected restaurant on selected date on selected sitting
cmd.CommandText = "select SUM(Number_Of_Seats) from RESERVATIONS where Sitting = '" + SelectedSitting + "' and Branch_ID = '" + SelectedBranch + "' and Date_Of_Booking = '" + SelectedDate + "' ";
//Assign the value of the people in the restaurant to a variable
int peopleinrestaurant = (int)cmd.ExecuteScalar();
//Query to find out how many people the selected restaurant seats
cmd.CommandText = "select SUM(Capacity) from BRANCH where Branch_ID = '" + SelectedBranch + "'";
//Assign the value of the people in the restaurant to a variable
int branchCapacity = (int)cmd.ExecuteScalar();
//add the amount of people in the party to the restaurant occupancy and assign to a variable
int totalOccupancy = peopleinrestaurant + SelectedDiners;
if (totalOccupancy <= branchCapacity)
{
//Show success message
SuccessFailureLabel.Visible = true;
SuccessFailureLabel.Text = "Booking available. Please proceed.";
//enable customer details text boxes so the customer can proceed
ConfirmBookingButton.Enabled = true;
}
else
{
//Show failure message if booking over capacity. Show user how many seats are available
SuccessFailureLabel.Visible = true;
SuccessFailureLabel.Text = "Cannot proceed. There are only " + (branchCapacity - peopleinrestaurant) + " seats available.";
}
}
}