【问题标题】:An expression of non-boolean type specified in a context where a condition is expected, near 'and'. C#在预期条件的上下文中指定的非布尔类型表达式,靠近“和”。 C#
【发布时间】:2018-04-01 22:41:25
【问题描述】:

我有一个网络表单,用户可以在其中预订导师。但他们必须首先选择课程/科目、日期和时间进行预订。我有一个通过数据库运行的方法来检查课程、日期和时间是否没有同时出现在表中。换句话说,它会检查预订是否可用,或者是否有人已经预订了该特定日期和时间的课程。

代码:

public void Availability(string course, string time, string date)
    {
        try
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tlcString"].ConnectionString))
            {
                string message = "Booking already exists";
                string query = "Select * From Bookings Where [Course=@course] and [Time=@time] and [Date=@date];";
                using (SqlCommand command = new SqlCommand(query, conn))
                {
                    command.Parameters.Add("@course", SqlDbType.NVarChar, 50).Value = course;
                    command.Parameters.Add("@time", SqlDbType.NVarChar, 50).Value = time;
                    command.Parameters.Add("@date", SqlDbType.DateTime, 50).Value = date;

                    conn.Open();

                    using (SqlDataReader dr = command.ExecuteReader())
                    {
                        if (dr.Read())
                            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);

                        dr.Close();
                    }
                    conn.Close();
                }
            }
        }

        catch
        {
            string error = "Error checking availability of booking";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
        }
    }

显示这个错误

在预期条件的上下文中指定的非布尔类型的表达式,靠近“和”

【问题讨论】:

  • 你为什么在[Course=@course]中使用这样的方括号?不要。

标签: c# mysql sql asp.net


【解决方案1】:

一些事情

  1. 修复括号,例如使用 [time]=@time 而不是 [time=@time]
  2. 检查课程、日期和时间是否为空/空值。
  3. 检查字符串变量“date”是否会转换为有效日期。

【讨论】:

  • 我进行了验证以确保所有变量都不能为空,并且我已经将日期转换为短日期。我想我会修复括号
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 2011-09-22
  • 2013-02-11
相关资源
最近更新 更多