Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
application for appointment

I have A gridview with these columns

AppId Appdate  Starttime Endtime Consumer


And 2 dropdownlists below the gridview to select start time and end time to fix the appointment.

I select the times and other values and click on insert button

Before inserting the new appointment I want to check the existing appointment on that time. if there is an appointment fixed on that time than it should give me some message. Otherwise the new appointment will be inserted

I wrote this code

C#
protected void BtnSetApp_Click(object sender, EventArgs e)
    {

        int statusid = Convert.ToInt32(scheduledid());
        sfid = Convert.ToInt32(Ddlsfo.SelectedValue.ToString());
       // DateTime appdate = Convert.ToDateTime(Txtappdate.ToString());
        DateTime starttime = Convert.ToDateTime(ddlstartdate.SelectedValue.ToString());
        DateTime endtime = Convert.ToDateTime(Ddlenddate.SelectedValue.ToString());
        //Perticular Sfo With Which The Appoint To Be Fixed
        //status = Convert.ToInt32(Ddlstatus.SelectedValue.ToString());// Set The Status
        appointmentgrid();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            //DateTime gsatrttime = Convert.ToDateTime(ds.Tables[0].Rows[i][2].ToString());
            //DateTime gendtime = Convert.ToDateTime(ds.Tables[0].Rows[i][2].ToString());
            if (starttime == Convert.ToDateTime(ds.Tables["t"].Rows[i][2].ToString()) && endtime == Convert.ToDateTime(ds.Tables["t"].Rows[i][3].ToString()))
            {

                lblmessg.Text = "Appointment time has ben taken,Select New Time For Appointment";

            }
            else
            {
                cn.Close();

                cmd = new SqlCommand("insert into CC_Appointment_Detail(Appointment_ID,Appointment_Date,Appointment_Desc,Appointment_Status_Id,SF_ID,User_ID,Start_Time,End_Time,Consumer_ID,Created_By,Created_Date,Updated_By,Last_Updated_Date,Valid) values (" + Txtappid.Text + ",'" + Txtappdate.Text + "','" + Txtdesc.Text + "'," + statusid + "," + sfid + "," + uid + ",'" + ddlstartdate.SelectedValue.ToString() + "','" + Ddlenddate.SelectedValue.ToString() + "'," + consumerid + ",'" + uname + "',getdate(),'" + uname + "',getdate(),'true')", cn);
                cmd.CommandType = CommandType.Text;
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
                lblmessg.Text = "Appointment Has been Set";
                Response.Redirect("callScreen.aspx");

            }

C#
public void appointmentgrid()
   {
       cn.Close();
       //select A.Appointment_ID,A.Appointment_Date,A.Start_Time,A.End_Time,C.First_Name from CC_Appointment_detail1 a , CC_Consumer_Detail C where a.Consumer_ID=c.Consumer_Id and  a.SF_ID=1 and a.Appointment_Date='5/6/12'
       da = new SqlDataAdapter("select A.Appointment_ID,A.Appointment_Date,A.Start_Time,A.End_Time,C.First_Name from CC_Appointment_Detail a,CC_Consumer_Detail C where  a.Consumer_ID=c.Consumer_Id and  a.SF_ID=" + Ddlsfo.SelectedValue.ToString() + " and a.Appointment_Date='" + Txtappdate.Text + "'", cn);
       da.Fill(ds,"t");
       GridView1.DataSource = ds.Tables["t"];
       GridView1.DataBind();
   }



This code not showing any error but it is inserting the appointment everytime.(only else part is getting executed).


please help.
Posted
Updated 9-May-12 5:01am
v3
Comments
Richard MacCutchan 9-May-12 9:31am    
only else part is getting executed
Then you need to get to work with your debugger and find out why your if statement is never returning TRUE.
Sandeep Mewara 9-May-12 10:35am    
:)

This simply means that either of below is false:
C#
starttime == Convert.ToDateTime(ds.Tables["t"].Rows[i][2].ToString()

OR
C#
endtime == Convert.ToDateTime(ds.Tables["t"].Rows[i][3].ToString())


If you are expecting both of them to be true in some scenario then DEBUG and see what's going wrong. Are you sure that the given table's row and index is fetching what you want to before comparing it? By chance are they reverse? Or some time difference?
 
Share this answer
 
To add to previous answer, you should never concatenate literals to SQL statements. This makes you vulnerable to SQL injections, data conversion mismatches etc. Use SqlParameter[^] instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900