返回null值时,Database Query崩溃

时间:2013-11-12 10:14:31

标签: c# asp.net sql

我正在尝试压缩我的网络应用程序中的错误。基本上我正在尝试实施餐厅预订系统。用户选择他们的聚会中的人数,坐(午餐或晚餐)和日期。然后系统查询我的数据库,让用户知道是否有可用性。如果在餐厅中已经预订了一些人,但是如果查询返回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.";
        }
    }
}

5 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

您可以使用

检查是否返回null
isnull(your query,replacement_value)

答案 2 :(得分:1)

您需要专门处理NULL值。 NULL不为0,因此您无法使用它进行计算。有几种方法可以处理NULL,但如果你想使用数字,那么一种方法就是将ISNULL()添加到sql查询中。

SUM(ISNULL(NumberOfSeats,0))

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

答案 3 :(得分:1)

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

select COALESCE(SUM(Number_Of_Seats), 0) from RESERVATIONS where ...

答案 4 :(得分:0)

变化

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

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

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

相关问题