2

I was doing an appointment function from web site. It can compare the date & time before create new appointment, if user key in the same date & time is exist in the db, it will pop out messagebox. When I try to insert different date & time which different from db, it give me error. Error

I getting error on this line :

string dtime = time.ExecuteScalar().ToString();

I have no idea what is wrong with my code, can anyone point me out? Thanks. This is my 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)
{
    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.");
        }
}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ching
  • 93
  • 2
  • 4
  • 13
  • 2
    In which line are you getting the error? – MBen Jan 06 '13 at 12:00
  • 1
    Are you sure `adate` field in database is not null ? I think It throws an exception because `adate` is null – Saber Amani Jan 06 '13 at 12:03
  • @MBen Sorry I have edit my post and include the error, i having error on string dtime = time.ExecuteScalar().ToString(); – Ching Jan 06 '13 at 12:03
  • @SaberAmani In my database the null is checked, I was trying to unchecked it and save but it don't allow me to save it. It this is the problem that cause me get my program error? – Ching Jan 06 '13 at 12:06
  • @Ching why are you calling con.Close before the if statement? I believe is you close it, it will be disposed (the first if (ddata...) – MBen Jan 06 '13 at 12:09
  • @Ching That's why you get exception, if it set to null in your database you have to check when you read from your database. – Saber Amani Jan 06 '13 at 12:10

3 Answers3

2

It seems that there is problem with ExcuteScalar(); because it returns null when there is no record exist for the query.

You can use like this

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

Detail

ExecuteScalar throws NullReferenceException

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
1

Try this

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();
        }
    }
}
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
  • I don't think connection is a problem here, because it reads data before closing connection – Saber Amani Jan 06 '13 at 12:12
  • that's correct but instead of executing 2time, assign it to a variable and check that in if condition – Saber Amani Jan 06 '13 at 12:15
  • @The Indian Programmer Hi, it still give me the same errors. – Ching Jan 06 '13 at 12:16
  • thats correct, I was in a quiet hurry – Kishore Kumar Jan 06 '13 at 12:16
  • @TheIndianProgrammmer Hi, it solve my problem already! thanks. It wont pop out error when I choose different date & time. But I add a function below to update my date & time for second attempt input, it seems get me back to the error. Any idea? Is it relate to my database because of I checked the null check box? – Ching Jan 06 '13 at 12:25
  • I can't upvote the answer because it said I need at least 15 rep to do this. – Ching Jan 06 '13 at 15:39
  • @TheIndianProgrammmer I got another error, I can insert different time, but not the date. It seems I need to insert same date from db and different time from db then only it can be created. – Ching Jan 06 '13 at 15:43
0

It looks like your problem is, that the time variable is empty when your call to ExecuteScalar does not return any values. just check if the time variable is valid (!= null) before you try to call it's ExecuteScalar member.