0

我正在尝试消除我的 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.";
        }
    }
}
4

5 回答 5

4

您应该在将值转换为 int 之前检查 null。

//Assign the value of the people in the restaurant to a variable
var obj = cmd.ExecuteScalar();

int peopleinrestaurant = obj != null ? (int)obj : 0;
于 2013-11-12T10:21:45.423 回答
1

您可以通过使用检查是否返回 null

isnull(your query,replacement_value)
于 2013-11-12T10:19:47.377 回答
1

您需要专门处理 NULL 值。NULL 不是 0,所以你不能用它做计算。有几种方法可以处理 NULL,但是如果您想处理数字,一种很好的方法是将 ISNULL() 添加到 sql 查询中。

SUM(ISNULL(NumberOfSeats,0))

但是,如前所述, SUM() 永远不应该返回 NULL 并且应该忽略 NULL,所以我认为您的问题不在那个地方。

于 2013-11-12T10:20:35.753 回答
1

在这种情况下,最简单的解决方案可能是不让您的查询返回 NULL:

select COALESCE(SUM(Number_Of_Seats), 0) from RESERVATIONS where ...
于 2013-11-12T10:22:09.370 回答
0

改变

int peopleinrestaurant = (int)cmd.ExecuteScalar();

int peopleinrestaurant = cmd.ExecuteScalar() == null ? 0 : (int)cmd.ExecuteScalar();

您的查询返回 null,问题不在于查询,而在于您在 c# 中的数据操作。

于 2013-11-12T10:26:13.477 回答