c#对象引用未设置为对象的实例

c#对象引用未设置为对象的实例,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我在网站上做预约功能。它可以在创建新约会之前比较日期和时间,如果数据库中存在相同日期和时间的用户密钥,则会弹出消息框。当我尝试插入与db不同的日期和时间时,它会给我错误。 我在这一行遇到错误: string dtime = time.ExecuteScalar().ToString(); 我不知道我的代码出了什么问题,有人能指出我吗?谢谢 这是我的代码: using System; using System.Collections.Generic; using System.Linq; us

我在网站上做预约功能。它可以在创建新约会之前比较日期和时间,如果数据库中存在相同日期和时间的用户密钥,则会弹出消息框。当我尝试插入与db不同的日期和时间时,它会给我错误。

我在这一行遇到错误:

string dtime = time.ExecuteScalar().ToString();
我不知道我的代码出了什么问题,有人能指出我吗?谢谢 这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
public partial class MakeAppointment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    string appointmentdate = Convert.ToString(DropDownListDay.Text + "-" + DropDownListMonth.Text + "-" + DropDownListYear.Text);
    string appointmenttime = Convert.ToString(DropDownListHour.Text + ":" + DropDownListMinute.Text + ":" + DropDownListSecond.Text + " " + DropDownListSession.Text);

    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");
    con.Open();
    SqlCommand date = new SqlCommand("Select adate from customer_registration where adate='"+ appointmentdate +"'",con);
    string ddate = date.ExecuteScalar().ToString();
    con.Close();
    if (ddate == appointmentdate)
    {
        con.Open();
        SqlCommand time = new SqlCommand("Select atime from customer_registration where atime='"+ appointmenttime +"'", con);
        string dtime = time.ExecuteScalar().ToString();
        con.Close();

        if (dtime == appointmenttime)
        {
            MessageBox.Show("This appointment is not available. Please choose other date & time.");
        }
}
}

看起来您的问题是,当调用ExecuteScalar不返回任何值时,时间变量为空。在尝试调用其ExecuteScalar成员之前,只需检查时间变量是否有效(!=null)。

试试这个

using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System;
public partial class MakeAppointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string appointmentdate = Convert.ToString(DropDownListDay.Text + "-" + DropDownListMonth.Text + "-" + DropDownListYear.Text);
        string appointmenttime = Convert.ToString(DropDownListHour.Text + ":" + DropDownListMinute.Text + ":" + DropDownListSecond.Text + " " + DropDownListSession.Text);

        using (SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True"))
        {
            con.Open();
            SqlCommand date = new SqlCommand("Select adate from customer_registration where adate='" + appointmentdate + "'", con);
            string ddate = date.ExecuteScalar().ToString();
            if (ddate == appointmentdate)
            {
                SqlCommand time = new SqlCommand("Select atime from customer_registration where atime='" + appointmenttime + "'", con);
                var rtime = time.ExecuteScalar();
                if (rtime != null)
                {
                    string dtime = rtime.ToString();

                    if (dtime == appointmenttime)
                    {
                        MessageBox.Show("This appointment is not available. Please choose other date & time.");
                    }
                }
                else
                {
                    MessageBox.Show("Failed to fetch time from database");
                }
            }
            con.Close();
        }
    }
}

似乎
ExcuteScalar()有问题null

你可以这样用

   var data= date.ExecuteScalar();
   if(data!=null)
         ddate =data.ToString();
细部


您在哪一行得到错误?您确定数据库中的
adate
字段不为空吗?我认为它抛出了一个异常,因为
adate
是null@MBen很抱歉,我编辑了我的帖子并包含了错误,我在字符串dtime=time.ExecuteScalar().ToString()上有错误@SaberAmani在我的数据库中,null被选中,我试图取消选中它并保存,但它不允许我保存它。这就是导致我的程序出错的原因吗?@Ching为什么在if语句之前调用con.Close?我相信如果你关闭它,它将被处理(第一个如果(ddata…)我不认为连接在这里是个问题,因为它在关闭连接之前读取数据这是正确的,但不是执行2time,而是将其分配给一个变量,并在condition@The印度程序员嗨,它仍然给我同样的错误。那是正确的,我在一个安静的地方hurry@TheIndianProgrammmer嗨,它已经解决了我的问题!谢谢s、 当我选择不同的日期和时间时,它不会弹出错误。但我在下面添加了一个函数来更新第二次尝试输入的日期和时间,这似乎让我回到了错误。有什么想法吗?它是否与我的数据库有关,因为我选中了空复选框?欢迎您,如果它有效,请接受该解决方案。