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.";
}
}
}