c#对象引用没有设置为对象的实例
本文关键字:对象 实例 设置 对象引用 | 更新日期: 2023-09-27 17:50:46
我正在做一个来自网站的约会功能。它可以比较日期&创建新约会前的时间,如果用户输入了相同的日期& &;如果数据库中存在时间,则会弹出消息框。当我尝试插入不同的日期&时间和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.");
}
}
}
似乎ExcuteScalar();
有问题,因为当查询没有记录存在时,它返回null
。
你可以这样写
var data= date.ExecuteScalar();
if(data!=null)
ddate =data.ToString();
详细ExecuteScalar抛出NullReferenceException
试试这个
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();
}
}
}
看起来您的问题是,当您调用ExecuteScalar不返回任何值时,时间变量为空。只需检查时间变量是否有效(!)= null),然后再尝试调用它的ExecuteScalar成员