Auto Time Table Part 28 Semester Sections in C-Sharp

Hi, Dear's here we learn how to implement Auto Time Table Generator in Visual Studio using C# Windows Form. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials.

In this tutorial we will cover all the below mention topics:
    i)      How to Create Semester Sections Form inside in ProgramSemesterForms Folder in our project?
    ii)     How to design Semester Sections Form?
 
    iii)    How to Insert, update, delete and retrieve records from SectionTable and show inside datagridview(dgv)?

So if you want all question answers watch the video, and for source code scroll down.


Design Semester Sections Form in Window Forms :
So in this example, we have form, show below in figure:


Source Code Semester Sections Form : 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TimeTableGenerator.SourceCode;

namespace TimeTableGenerator.Forms.ProgramSemesterForms
{
    public partial class frmSemesterSections : Form
    {
        public frmSemesterSections()
        {
            InitializeComponent();
        }

        //Retrive All Semester Sections and Show in dgvSections datagridview in from of list
        public void FillGrid(string searchvalue)
        {

            try
            {
                string query = string.Empty;
                if (string.IsNullOrEmpty(searchvalue.Trim()))
                {
                    query = " select SectionID, SectionTitle [Section], ProgramSemesterID, Title [Semester],IsActive [Status] from v_AllSemesterSections order by ProgramSemesterID ";
                }
                else
                {
                    query = " select SectionID, SectionTitle [Section], ProgramSemesterID, Title [Semester],IsActive [Status] from v_AllSemesterSections " +
                            " WHERE (SectionTitle + ' ' + Title) like '%" + searchvalue.Trim() + "%' order by ProgramSemesterID";
                }

                DataTable sectionlist = DatabaseLayer.Retrive(query);
                dgvSections.DataSource = sectionlist;
                if (dgvSections.Rows.Count > 0)
                {
                    dgvSections.Columns[0].Visible = false; // SectionID
                    dgvSections.Columns[1].Width = 200; //SectionTitle
                    dgvSections.Columns[2].Visible = false; // ProgramSemesterID
                    dgvSections.Columns[3].Width = 200; // Title
                    dgvSections.Columns[4].Width = 80; // IsActive

                    dgvSections.ClearSelection();
                }
            }
            catch
            {
                MessageBox.Show("Some unexpected issue occure plz try again!");
            }
        }

        // Clear Form or Rest Form Controls
        private void FormClear()
        {
            txtTitle.Clear();
            cmbSemesters.SelectedIndex = 0;
        }

        //save button code to save unique record in sectiontable
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                if (txtTitle.Text.Trim().Length == 0)
                {
                    ep.SetError(txtTitle, "Please Enter Section Title!");
                    txtTitle.Focus();
                    return;
                }

                if (cmbSemesters.SelectedIndex == 0)
                {
                    ep.SetError(cmbSemesters, "Please Select Semester!");
                    cmbSemesters.Focus();
                    return;
                }
                // Check Existing Record, if found show "Already Exists" message near txtTitle
                DataTable dt = DatabaseLayer.Retrive("select * from SectionTable where SectionTitle = '" + txtTitle.Text.Trim() + "' and ProgramSemesterID = '" + cmbSemesters.SelectedValue + "'");
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        ep.SetError(txtTitle, "Already Exists!");
                        txtTitle.Focus();
                        return;
                    }
                }
                // Query for save/insert record in sectiontable
                string insertquery = string.Format("insert into SectionTable(SectionTitle,ProgramSemesterID) values('{0}','{1}')", txtTitle.Text.Trim(), cmbSemesters.SelectedValue);
                bool result = DatabaseLayer.Insert(insertquery);
                if (result == true)
                {
                    MessageBox.Show("Save Successfully!");
                    FillGrid(string.Empty);
                    FormClear();
                }
                else
                {
                    MessageBox.Show("Please Try Again!");
                }
            }
            catch (Exception ex) // Show Error message in any case if exception generate
            {
                MessageBox.Show(ex.Message);
            }

        }

        // Load AllPorgrames with semester in combo list and fill dgvSections
        private void frmSemesterSections_Load(object sender, EventArgs e)
        {
            ComboHelper.AllProgramSemesters(cmbSemesters);
            FillGrid(string.Empty);
        }

        //Clear Button to Clear Form
        private void btnClear_Click(object sender, EventArgs e)
        {
            FormClear();
        }

        // Search Semester or Section
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            FillGrid(txtSearch.Text.Trim());
        }
        
        //Edit Selected Record
        private void cmsedit_Click(object sender, EventArgs e)
        {
            if (dgvSections != null)
            {
                if (dgvSections.Rows.Count > 0)
                {
                    if (dgvSections.SelectedRows.Count == 1)
                    {

                        if (MessageBox.Show("Are you sure you want to change status?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            bool existstatus = Convert.ToBoolean(dgvSections.CurrentRow.Cells[4].Value);
                            int sectionid = Convert.ToInt32(dgvSections.CurrentRow.Cells[0].Value);
                            bool status = false;
                            if (existstatus == true)
                            {
                                status = false;
                            }
                            else
                            {
                                status = true;
                            }
                            string updatequery = string.Format("update SectionTable set IsActive = '{0}' where SectionID = '{1}'", status, sectionid);
                            bool result = DatabaseLayer.Update(updatequery);
                            if (result == true)
                            {
                                MessageBox.Show("Change Successfully!");
                                FillGrid(string.Empty);
                            }
                            else
                            {
                                MessageBox.Show("Please Try Again!");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Select One Record!");
                    }
                }
                else
                {
                    MessageBox.Show("List is Empty!");
                }
            }
            else
            {
                MessageBox.Show("List is Empty!");
            }
        }

        // Numaric text box
        private void txtCapacity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
       (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
        }
    }
}

Comments

  1. thank u sir
    i have a question in this video the session is added to program semester title but in previous video of program semester table the title only contained program and semester should i add session title combo box to program semester table???

    ReplyDelete

Post a Comment