articles

Home / DeveloperSection / Articles / Application to store appointment, in C#

Application to store appointment, in C#

Application to store appointment, in C#

Sachindra Singh 17382 21-Jan-2011

This program basically base on store appointment or any kind of notes in selected month and year. It will show which month and year have appointment, notes or information as well we can add, delete and get data.

Example form show in which date is appointment if appointment is in many months select month from month combo box as well as year, if appointment record is available date color will be red as shown below:

Application to store appointment, in C#

using System.Data.SqlClient;
 
namespace UserDefineControls
{
    public partialclassExample : Form
    {
        publicExample()
        {
            InitializeComponent();
        }
        intx=5,y=20;// Creating 2 int member variable for x-axis and y-axis for RichtextBox
        SqlConnection con;// con is a member variable of SqlConnection class
        RichTextBox[] rtb;//rtb  is member variable RichTextBox
        string[] m_smonth= { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };// m_smonth is array that store name of months
        int[] m_nMonthDays= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//m_nMonthDays is array that store days of month
        string[] sdays= { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };//sdays is a array that store days name
        DateTime dt;//dt is member variable
        intyear, month, day;//year,month,day variable store year ,month and day
        privatevoidExample_Load(objectsender, EventArgse)
        {
            con=newSqlConnection("Data Source=uttam-pc\\mindstick; user id=sa; password=mindstick; initial catalog=Detail");//creating connection
            for (inti=0; i<m_smonth.Length; i++)//for loop will execute less than of m_smonth length
            {
                cbMonth.Items.Add(m_smonth[i]);//adding month in cbMonth(combobox)
            }
            for (intc=2000; c<=2020; c++)//for loop execute from 2000 and less than equal 2020
            {
                cbYear.Items.Add(c);//adding year in cbYear(combobox)
            }
            day=m_nMonthDays[0];//day initializing m_nMonthDays which index is 0
            year=DateTime.Today.Date.Year;//year storing current year from DateTime which property is Today which property is Date which property is year
            month=DateTime.Today.Date.Month;//month variable storing current from DateTime which property is Today which property is Date which property is month
            AddRichTextBox(year,month,1);
        }
        publicvoidAddRichTextBox(intyear,intmonth,intdate)//creating function which have three parameter year,month and date
        {
            Detail.sappointdetailYear=year.ToString();//storing year value in static variable sappointdetailYear which is declared in Detail class
            dt=newDateTime(year, month, date);//creating object of DateTime class with three parameter
            for (inti=0; i<sdays.Length; i++)//loop executing less than length of sdays array
            {
                if (dt.DayOfWeek.ToString() ==sdays[i])//checking which day is equal to sdays index
                    break;//loop will be terminate
                else
                    x+=85;//adding 85 in x it will decide the x-axis
            }
            rtb=newRichTextBox[day];//creating object of RichTextBox day varaiable decide how many RichTextBox will be create
            intc=1;//c varaible intializing
            for (inti=0; i<rtb.Length; i++)//loop executing less than length of RichTextBox array rtb is a object of RichTextBox
            {
                rtb[i] =newRichTextBox();//creating object
                rtb[i].Name=i.ToString();//giving name of each RichTextBox
                rtb[i].Text=c.ToString();
                rtb[i].SetBounds(x, y, 80, 80);//this line decide where RichTexBox will be show
                c++;//increment of 1
                x +=85;//increment of 85 in x
                if (x>590)//checking condition x is less than 590
                {
                    x=5;//store 5 in x
                    y +=85;//y increment by 85
                }
                pRichtextBox.Controls.Add(rtb[i]);//adding RichtextBox in panel(pRichtextBox)
            }
            GetNotes();//this is function
        }
        privatevoid cbMonth_SelectedIndexChanged(objectsender, EventArgse)
        {
            day=m_nMonthDays[cbMonth.SelectedIndex];//this line store how many days in a month
            if (DateTime.IsLeapYear(year))//checking year is a leap year or not
            {
                if (cbMonth.SelectedItem.ToString() =="February")//if month is february day will be increment by 1
                    day++;//increment by 1
            }
            PanelClear();//this a function
            month=Convert.ToInt32(cbMonth.SelectedIndex+1);//month is storing index cbMonth(combobox) increment by 1
 
            AddRichTextBox(year, cbMonth.SelectedIndex+1, 1);//this is a function  with three parameter year,index of cbMonth(combobox)increment by 1 and 1 is date
        }
        privatevoid cbYear_SelectedIndexChanged(object sender, EventArgse)
        {
           
        }
        publicvoidPanelClear()//panel clear function clear all controls from panel
        {
            pRichtextBox.Controls.Clear();
            x=5;//assigning value
            y=20;//assigning value
        }
        private void cbYear_SelectedValueChanged(object sender, EventArgse)
        {
            PanelClear();//this a function
            year=Convert.ToInt32(cbYear.SelectedItem);//year storing year from combobox(cbYear)
            AddRichTextBox(cbYear.SelectedIndex+ 1, month, 1);//passing value to AddRichTextBox function
        }
        public voidGetNotes()//creating function
        {
           
            try
            {
                con.Open();//connection opening
                SqlCommand cmd= new SqlCommand("select days from AppointmentDetail  where year='"+year+"' and Month='"+month+"'", con);//this line return day in which have information
                SqlDataReader dr= cmd.ExecuteReader();//dr storing data
                while (dr.Read())//it will execute untill this condition become false
                {
                    int count= Convert.ToInt32(dr[0].ToString());//storing date in count variable
                    rtb[count-1].ForeColor=Color.Red;//this fill Forecolor of RichtextBox text
                    rtb[count-1].Font=newFont("Calista", 14, FontStyle.Italic);//Changing font style of Richtextbox
                    rtb[count-1].Click+=newEventHandler(rtb_Click);// generating click event on richtexbox that show information of date
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);//showing execption with the help of popup
            }
            finally
            {
                con.Close();//closing connection
            }
        }
        RichTextBox br;//creating variable of RichTextBox
        public voidrtb_Click(objectsender, EventArgse)
        {
            br= (RichTextBox)sender;//this line store reference on which richtextbox is clicked
            int c=0;//initailizing variable
            for (; br!=rtb[c]; c++) ;//searching which RichTextBox is clicked
            AppointmentDetail appointdetail=new AppointmentDetail();//Creating object of AppointmentDetail form
            Detail.sappointdetailDay=br.Text;//storing RichTextBox text  in static variable sappointdetailDay which is declared in Detail class
            Detail.sappointdetailYear=year.ToString();//storing year in static variable sappointdetailYear which is declared in Detail class
            appointdetail.ShowDialog();
        }
 
        private void cbAppointmentAddDelete_SelectedIndexChanged(objectsender, EventArgse)
        {
            if (cbAppointmentAddDelete.SelectedItem.ToString() == "Add")//checking condition
            {
                AddAppointment addapp= new AddAppointment();//Creating object of AddAppointment form for new  information, notes or appointment
                addapp.Show();//it will show form
            }
            else if (cbAppointmentAddDelete.SelectedItem.ToString() == "Delete")//checking condition
            {
                DeleteAppointmentdeletapp=new DeleteAppointment();//Creating object of AddAppointment form for delete information, notes or appointment
                deletapp.Show();
            }
        }
      
    }
}

 

AppDetail form show appointment on which data forecolor is red, red color show


that day record is available as well as show date and year as shown in below.

 

Application to store appointment, in C#

 

using System.Data.SqlClient;
 
namespace UserDefineControls
{
    public partialclass AppointmentDetail : Form
    {
        publicAppointmentDetail()
        {
            InitializeComponent();
        }
        SqlConnection con;// con is a member variable
        private void AppointmentDetail_Load(object sender, EventArgse)
        {
          
            this.Text="Date--"+Detail.sappointdetailDay+"  "+"Year--"+ Detail.sappointdetailYear;//defining form name
            con=newSqlConnection("Data Source=uttam-pc\\mindstick; user id=sa; password=mindstick; initial catalog=Detail");//creating connection
            try
            {
                string data= "";//creating string variable
                con.Open();
                SqlCommand cmd= new SqlCommand("select AppDetail from AppointmentDetail where days='"+ Detail.sappointdetailDay+ "' and year='"+ Detail.sappointdetailYear+ "'", con);//this command return appointment detail
                SqlDataReader dr= cmd.ExecuteReader();//dr storing data
                if (dr.Read())//checking data avialbel or not
                {
                    data=dr[0].ToString();//sotring data in data variable
                }
                if (data== "")//checking condition
                    richTextBox1.Text="No Records";//set no records in richTextBox1
                else
                    richTextBox1.Text=data;//printing data inrichTextBox1
            }           
            catch (Exception ex)
            {
 
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }
 
        }
    }
}

 

AddAppointment form to use for add appointment, notes or information from AddAppointment we can choose year, month and date where we want on which date to add appointment, notes box provides to write add appointment ,if we click on the save button appointment will be save if we click on the cancel button form will be close.


Application to store appointment, in C#


using System.Data.SqlClient;
 
namespace UserDefineControls
{
    public partialclass AddAppointment : Form
    {
        publicAddAppointment()
        {
            InitializeComponent();
           
        }
        string[] m_smonth= { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };//m_smonth is array that store name of months
        int[] m_nMonthDays= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//m_nMonthDays is array that store days of month
        SqlConnection con;//con is a member variable
        intleap,days;//creating int type variable
    
        private voidbutton1_Click(objectsender, EventArgse)
        {
            try
            {
                int month= cbmonth.SelectedIndex;//storing index of month
                month++;//increment by 1
                con.Open();//openning connection
                SqlCommand cmd= new SqlCommand("insert AppointmentDetail values('"+cbDate.Text+ "','"+cbYear.Text+"','"+rtbNotes.Text+"','"+month+"')", con);//insert command executing
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
       private void AddAppointment_Load(objectsender, EventArgse)
       {
           cbYear.SelectedText= (DateTime.Today.Date.Year.ToString());//this line set current year in   cbYear(combobox)
           cbDate.SelectedText= (DateTime.Today.Day.ToString());//this line set current date in   cbDate(combobox)
           cbmonth.SelectedText= (m_smonth[DateTime.Today.Month-1]);//this line set current month in   cbmonth(combobox)
            con=newSqlConnection("Data Source=uttam-pc\\mindstick; user id=sa; password=mindstick; initial catalog=Detail");//creating connection
            for (intc=2000; c<=2020; c++)
            {
                cbYear.Items.Add(c);//adding year in abyear
            }
            for (inti=0; i<m_smonth.Length; i++)
            {
                cbmonth.Items.Add(m_smonth[i]);//adding month in  cbmonth
            }
        }
        private void cbYear_SelectedIndexChanged(object sender, EventArgse)
        {
             leap=Convert.ToInt32( cbYear.SelectedItem.ToString());//stroring year in leap for check is year leap or not
        }
        private void cbmonth_SelectedIndexChanged(objectsender, EventArgse)
        {
            cbDate.Items.Clear();//clearing items from cbDate
            days=cbmonth.SelectedIndex;//stroing index from cbmonth
            days=m_nMonthDays[days];//storing days index of m_nMonthDays
            if (DateTime.IsLeapYear(leap))//checking year is leap or not
            {
                days++;//increment by 1
            }
            for (intc=1; c<=days; c++)
            {
                cbDate.Items.Add(c);//adding date in cbDate
            }
        }
        private voidbtn_Click(objectsender, EventArgse)
        {
            this.Close();//form close
        }
        private void cbDate_SelectedIndexChanged(object sender, EventArgse)
        {
 
        }
    }
}

 

DeleteAppointment form will display all appointment, days, month in which appointment is held , when we to delete data from records click on the row, row will be select when you want to delete click on the delete button if we want to close window click on cancel button

 

Application to store appointment, in C#

using System.Data.SqlClient;
 
namespace UserDefineControls
{
    public partialclass DeleteAppointment : Form
    {
        publicDeleteAppointment()
        {
            InitializeComponent();
        }
        SqlConnection con;// con is a member variable
        private void DeleteAppointment_Load(object sender, EventArgse)
        {
            con=newSqlConnection("Data Source=uttam-pc\\mindstick; user id=sa; password=mindstick; initial catalog=Detail");//creating connection
           try
            {
                con.Open();
                SqlCommand cmd= new SqlCommand("Select AppDetail,days,Month from AppointmentDetail where year='"+ Detail.sappointdetailYear+ "'", con);//executing sql query it will return AppDetail,days and Month
                SqlDataReader dr= cmd.ExecuteReader();//dr storing data
                DataTable dt= newDataTable();//creating object of dt
                dt.Load(dr);//dt load dr data
                dataGridView1.DataSource=dt;//this line show data in datagridview
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
 
        private voidbutton1_Click(objectsender, EventArgse)
        {
            string day, month;//creating string type variables
           
            int index;//creating index to find datagridview index on which row is selected
            foreach (DataGridViewRow dr indataGridView1.SelectedRows)//executing loop
            {
                if (dr.Index!= dataGridView1.Rows.Count-1)//checking condition
                {
                 
                    index=dr.Index;//storing index of dr in index variable
                    day =dataGridView1.Rows[index].Cells[1].Value.ToString();//storing day from datagridview for delete operation perform
                    month=dataGridView1.Rows[index].Cells[2].Value.ToString();//storing month from datagridview for delete operation perform
                    dataGridView1.Rows[index].Cells[0].Value=null;//datagridview selected cell will be blank
                    dataGridView1.Rows[index].Cells[1].Value=null;//datagridview selected cell will be blank
                    dataGridView1.Rows[index].Cells[2].Value=null;//datagridview selected cell will be blank
                    dataGridView1.Rows[index].Cells[3].Value=null;//datagridview selected cell will be blank
                   try
                    {
                        con.Open();//opening connection
                        SqlCommandcmd= new SqlCommand("delete AppointmentDetail where days='"+day+ "' and Month='"+month +"'", con);//delete opertion performing
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exceptionex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                    }
 
                }
            }
        }
   
        privatevoidbutton2_Click(objectsender, EventArgse)
        {
            this.Close();//form close
        }
 
        privatevoid dataGridView1_CellContentClick(objectsender, DataGridViewCellEventArgse)
        {
         
        }
        privatevoid dataGridView1_CellMouseClick(objectsender, DataGridViewCellMouseEventArgse)
        {
          
         }
        }
    }

 

SQL Server is used to store records for appointments.

Application to store appointment, in C#



Updated 14-Feb-2020

Leave Comment

Comments

Liked By