Click here to Skip to main content
13,345,254 members (51,048 online)
Rate this:
 
Please Sign up or sign in to vote.
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

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");

            }

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 9-May-12 3:18am
Updated 9-May-12 6: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
   
:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

OR
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?
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 9 May 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100