Friday, September 21, 2012

C# Database Tutorials Using Visual Studio 2015 (Code) - Manage Students Form


The complete code is below

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 System.Configuration;
using System.Data.SqlClient;

namespace DatabaseDemo2
{
    public partial class ManageStudentsForm : Form
    {
        public ManageStudentsForm()
        {
            InitializeComponent();
        }

        private DataTable dtStudents = new DataTable();

        private void ManageStudentsForm_Load(object sender, EventArgs e)
        {
            LoadDataIntoDataGridView();
        }

        private void LoadDataIntoDataGridView()
        {
            dtStudents = GetStudentsList();
            StudentListDataGridView.DataSource = dtStudents;
        }

        private DataTable GetStudentsList()
        {
            DataTable dtStudents = new DataTable();

            string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand("usp_StudentGetAllStudents", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    conn.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

                    dtStudents.Load(reader);
                }
            }

            return dtStudents;
        }

        private void addNewStudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowStudentInfoForm(0, false);
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void NameTextBox_TextChanged(object sender, EventArgs e)
        {
            //DataView dvStudents = dtStudents.DefaultView;

            //dvStudents.RowFilter = "Name LIKE '%" + NameTextBox.Text + "%'";

            FilterStringByColumn("Name", NameTextBox);
        }

        private void EmailTextBox_TextChanged(object sender, EventArgs e)
        {
            //DataView dvStudents = dtStudents.DefaultView;

            //dvStudents.RowFilter = "Email LIKE '%" + EmailTextBox.Text + "%'";

            FilterStringByColumn("Email", EmailTextBox);
        }

        private void FilterStringByColumn(string columnName, TextBox txt)
        {
            DataView dvStudents = dtStudents.DefaultView;

            dvStudents.RowFilter = columnName + " LIKE '%" + txt.Text + "%'";
        }

        private void AddressTextBox_TextChanged(object sender, EventArgs e)
        {
            FilterStringByColumn("Address", AddressTextBox);
        }

        private void resetFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NameTextBox.Clear();
            EmailTextBox.Clear();
            AddressTextBox.Clear();
        }

        private void StudentListDataGridView_DoubleClick(object sender, EventArgs e)
        {
            int rowToUpdate = StudentListDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
            int studentId = Convert.ToInt16(StudentListDataGridView.Rows[rowToUpdate].Cells["StudentId"].Value);

            ShowStudentInfoForm(studentId, true);
        }

        private void ShowStudentInfoForm(int studentId, bool isUpdate)
        {
            StudentInfoForm sif = new StudentInfoForm();
            sif.StudentId = studentId;
            sif.IsUpdate = isUpdate;
            sif.ShowDialog();

            LoadDataIntoDataGridView();
        }
    }
}

Stored Procedure: usp_GetAllListData

CREATE PROCEDURE [dbo].[usp_StudentGetAllStudents]
AS
 BEGIN

  SELECT  st.[StudentId]
    ,st.[Name]
    ,st.[Email]
    ,CASE st.[GenderId]
     WHEN 1 THEN 'Male'
     WHEN 2 THEN 'Female'
     END AS 'Gender'
    ,CONVERT(varchar, st.[DateOfBirth], 103) AS 'Date Of Birth'
    ,ISNULL(st.[Address], '') + ' ' + ISNULL(l.[Description], '') + ' ' + ISNULL(c.[Description], '') + ' ' + ISNULL(st.[PostCode], '') AS 'Address'
  FROM [dbo].[Students] st
  LEFT JOIN [dbo].[Localities] l ON st.[LocalityId] = l.[LocalityId]
  LEFT JOIN [dbo].[Cities] c ON st.[CityId] = c.[CityId]

 END
GO

No comments:

Post a Comment