我正在從網站上執行預約功能。它可以比較日期&創建新約會之前的時間,如果用戶鍵在同一日期&時間存在於數據庫中,則會彈出消息框。當我嘗試插入不同於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.");
}
}
}
在哪一行,你得到的錯誤? – MBen
您確定數據庫中的adate'字段不爲空嗎?我認爲它拋出了一個異常,因爲'adate'爲空 – saber
@MBen對不起,我編輯我的文章,幷包括錯誤,我有錯誤的字符串dtime = time.ExecuteScalar()。 – Ching