2013-01-06 87 views
2

我正在从网站上执行预约功能。它可以比较日期&创建新约会之前的时间,如果用户键在同一日期&时间存在于数据库中,则会弹出消息框。当我尝试插入不同于db的日期&时,它给我错误。 Errorc#未将对象引用设置为对象的实例

我得到错误在这条线:

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."); 
     } 
} 
} 
+2

在哪一行,你得到的错误? – MBen

+1

您确定数据库中的adate'字段不为空吗?我认为它抛出了一个异常,因为'adate'为空 – saber

+0

@MBen对不起,我编辑我的文章,并包括错误,我有错误的字符串dtime = time.ExecuteScalar()。 – Ching

回答

1

试试这个

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(); 
     } 
    } 
} 
+0

我不认为连接在这里是一个问题,因为它在关闭连接前读取数据 – saber

+0

已更新之前的代码 –

+0

这是正确的,但不是执行2time,将它分配给一个变量并检查是否存在条件 – saber

0

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

2

似乎存在与ExcuteScalar();问题,因为它返回null当有此查询的记录存在。

您可以使用这样

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

详细

ExecuteScalar throws NullReferenceException

+0

欢迎您,如果它的工作,请接受解决方案。 –

相关问题