2013-01-06 84 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

歡迎您,如果它的工作,請接受解決方案。 –

相關問題