0
string date = DateTime.Now.AddDays(day - 1).ToShortDateString().ToString();
string count = "select count(*) from Appointment_Info where  APPT_Dt=\'" +
               Convert.ToDateTime(date) + "\'  ";
SqlCommand cc = new SqlCommand(count, cn);
int appoinments = Convert.ToInt16( cc.ExecuteScalar().ToString());

above query does not work plese see and tell is their any problems?

2
  • 3
    Use a parameter, it's really better than formatting dates... Commented Nov 2, 2011 at 8:52
  • 1
    @sikender: I suppose that you meant to say You need to specify what kind of error you get. Commented Nov 2, 2011 at 8:56

3 Answers 3

2

What you're after is such SQL instead:

DateTime dtFrom = DateTime.Now.AddDays(day - 1).Date;
DateTime dtTo = past.AddDays(1);
string strSQL = "Select Count(*) From Appointment_Info Where  APPT_Dt Between @from And @to";
int appoinments = 0;
using (SqlCommand cc = new SqlCommand(strSQL, cn))
{
    cc.Parameters.AddWithValue("@from", dtFrom);
    cc.Parameters.AddWithValue("@to", dtTo);
    appoinments = Int32.Parse(cc.ExecuteScalar().ToString());
}

Problem is you don't need exact date as it won't give you anything, you rather need range of dates meaning anything between the past date and day after it.

The above code also give better practice of using parameters and properly disposing the Command object.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this will fix your problem:

    string date = DateTime.Now.AddDays(day - 1).ToShortDateString().ToString();
    string count = "select count(*) from Appointment_Info where  convert(int,convert(varchar, APPT_Dt,112))=convert(int,convert(varchar, @date,112)) ";

    SqlCommand cc = new SqlCommand(count, cn);
    cc.parameters.AddWithValue("@date",date);
    int appoinments = Convert.ToInt16( cc.ExecuteScalar().ToString());

Comments

0

Try this:

DateTime dt = DateTime.Now.AddDays(day - 1);
SqlCommand cc = new SqlCommand(
    "SELECT COUNT(*) FROM Appointment_Info WHERE APPT_Dt=@dt", cn);
cc.Parameters.AddWithValue("@dt", dt);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.