c # Référence d'object non définie sur une instance d'un object

Je faisais une fonction de rendez-vous à partir du site Web. Il peut comparer la date et l'heure avant de créer un nouveau rendez-vous, si la key de l'user dans la même date et l'heure existe dans la database, il apparaîtra messagebox. Quand j'essaie d'insert une date et une heure différentes de db, cela me donne une erreur. Erreur

Je reçois une erreur sur cette ligne:

ssortingng dtime = time.ExecuteScalar().ToSsortingng(); 

Je n'ai aucune idée de ce qui ne va pas avec mon code, quelqu'un peut-il me signaler? Merci. C'est mon code:

 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) { ssortingng appointmentdate = Convert.ToSsortingng(DropDownListDay.Text + "-" + DropDownListMonth.Text + "-" + DropDownListYear.Text); ssortingng appointmenttime = Convert.ToSsortingng(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); ssortingng ddate = date.ExecuteScalar().ToSsortingng(); con.Close(); if (ddate == appointmentdate) { con.Open(); SqlCommand time = new SqlCommand("Select atime from customer_registration where atime='"+ appointmenttime +"'", con); ssortingng dtime = time.ExecuteScalar().ToSsortingng(); con.Close(); if (dtime == appointmenttime) { MessageBox.Show("This appointment is not available. Please choose other date & time."); } } } 

Essaye ça

 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) { ssortingng appointmentdate = Convert.ToSsortingng(DropDownListDay.Text + "-" + DropDownListMonth.Text + "-" + DropDownListYear.Text); ssortingng appointmenttime = Convert.ToSsortingng(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); ssortingng ddate = date.ExecuteScalar().ToSsortingng(); if (ddate == appointmentdate) { SqlCommand time = new SqlCommand("Select atime from customer_registration where atime='" + appointmenttime + "'", con); var rtime = time.ExecuteScalar(); if (rtime != null) { ssortingng dtime = rtime.ToSsortingng(); 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(); } } } 

Il semble qu'il y ait un problème avec ExcuteScalar(); car il renvoie null lorsqu'il n'existe aucun logging pour la requête.

Vous pouvez utiliser comme ça

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

Détail

ExecuteScalar renvoie NullReferenceException

Votre problème semble être que la variable d'heure est vide lorsque votre appel à ExecuteScalar ne renvoie aucune valeur. vérifiez simplement si la variable d'heure est valide (! = null) avant d'essayer d'appeler son membre ExecuteScalar.