07 Laboratory Exercise 1 ARGS FOR Event Driven Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

OUTPUTS

FOR

07 Laboratory Exercise 1 - ARG


I created the database using Serviced-Based Database. In th visual studio, at first I thought I have to use
micfrosoft sql database server, but then I realized ive been working on the wrong software all along
HAHAH

THE CODES

The FrmClubRegistration codes:

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;

namespace FrmClubRegistrations
{
public partial class FrmClubRegistration : Form
{
private ClubRegistrationQuery clubRegistrationQuery;
private int ID, Age;
private int count = 0;
private string FirstName, MiddleName, LastName, Gender, Program;
private long StudentId;
public FrmClubRegistration()
{
InitializeComponent();
}
private void Registerbutton1_Click(object sender, EventArgs e)
{
ID = RegistrationID();
StudentId = Convert.ToInt64(txtStudent.Text);
FirstName = txt1FirstName.Text;
MiddleName = txt1MiddleName.Text;
LastName = txt1LastName.Text;
Age = Convert.ToInt32(txt1Age.Text);
Gender = cbGender.Text;
Program = cboProgram.Text;
clubRegistrationQuery.RegisterStudent(ID, StudentId, FirstName,
MiddleName, LastName, Age, Gender, Program);
}
private void Update_Click(object sender, EventArgs e)
{
FrmUpdateMember woah = new FrmUpdateMember();
woah.Show();
}
private void Refresh_Click(object sender, EventArgs e)
{
RefreshListofClubMembers();
}
private void FrmClubRegistration_Load(object sender, EventArgs e)
{
clubRegistrationQuery = new ClubRegistrationQuery();
RefreshListofClubMembers();
}
public int RegistrationID()
{
return count++;
}
public void RefreshListofClubMembers()
{
clubRegistrationQuery.DisplayList();
dataGridView1.DataSource = clubRegistrationQuery.bindingSource;
}
}
}

The FrmUpdateMember

using System;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace FrmUpdateMember
{

public partial class Form2 : Form


{
private FrmClubRegistration.ClubRegistrationQuery clubRegistrationQuery;
private int Age;
private string Program;
private long StudentId;

public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
clubRegistrationQuery = new FrmClubRegistration.ClubRegistrationQuery();
clubRegistrationQuery.DisplayID(comboxID);
}

public void Fill()


{
string ClubDBConnectionString = @"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ClubDB.mdf;Integrated
Security=True";
SqlConnection sqlConnect = new SqlConnection(ClubDBConnectionString);
string getId = "SELECT * FROM ClubMembers";
SqlCommand sqlCommand = new SqlCommand(getId, sqlConnect);
sqlConnect.Open();
SqlDataReader sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
comboxID.Items.Add(sqlReader["StudentId"]);

}
sqlConnect.Close();
}
public void cbFill()
{
clubRegistrationQuery.DisplayID(comboxID);

}
public void TextFill()
{
LastNametxt.Text = clubRegistrationQuery._LastName;
FirstNametxt.Text = clubRegistrationQuery._FirstName;
Middletxt.Text = clubRegistrationQuery._MiddleName;
Agetxt.Text = clubRegistrationQuery._Age.ToString();
cbGender.Text = clubRegistrationQuery._Gender;
cbPrograms.Text = clubRegistrationQuery._Program;
}
private void comboxID_SelectedIndexChanged(object sender, EventArgs e)
{
clubRegistrationQuery.DisplayText(comboxID.Text);
TextFill();
}
private void Confirmbutton_Click(object sender, EventArgs e)
{
StudentId = Convert.ToInt64(comboxID.Text);
Age = Convert.ToInt32(Agetxt.Text);
Program = cbPrograms.Text;
clubRegistrationQuery.UpdateStudent(StudentId, Age, Program);
}
}
}

ClassRegistrationQuery

using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace FrmClubRegistration
{
class ClubRegistrationQuery
{
private SqlConnection sqlConnect;
private SqlCommand sqlCommand;
private SqlDataAdapter sqlAdapter;
private SqlDataReader sqlReader;
public DataTable dataTable;
public BindingSource bindingSource;
private string connectionString;
public string _FirstName, _MiddleName, _LastName, _Gender, _Program;
public int _Age;
public ClubRegistrationQuery()
{
connectionString = @"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ClubDB.mdf;Integrated
Security=True";
sqlConnect = new SqlConnection(connectionString);
dataTable = new DataTable();
bindingSource = new BindingSource();

}
public bool DisplayList()
{
string ViewClubMembers = "SELECT StudentId, FirstName,
MiddleName,LastName, Age, Gender, Program FROM ClubMembers";
sqlAdapter = new SqlDataAdapter(ViewClubMembers, sqlConnect);
dataTable.Clear();
sqlAdapter.Fill(dataTable);
bindingSource.DataSource = dataTable;
return true;
}
public bool RegisterStudent(int ID, long StudentID, string FirstName, string
MiddleName, string LastName, int Age, string Gender, string Program)
{
sqlCommand = new SqlCommand("Insert Into ClubMembers
VALUES(@ID, @StudentID, @FirstName, " +

"@MiddleName, @LastName, @Age, @Gender, @Program)", sqlConnect);


sqlCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
sqlCommand.Parameters.Add("@RegistrationID", SqlDbType.BigInt).Value
= StudentID;
sqlCommand.Parameters.Add("StudentID", SqlDbType.VarChar).Value =
StudentID;
sqlCommand.Parameters.Add("FirstName", SqlDbType.VarChar).Value =
FirstName;
sqlCommand.Parameters.Add("MiddleName", SqlDbType.VarChar).Value =
MiddleName;
sqlCommand.Parameters.Add("LastName", SqlDbType.VarChar).Value =
LastName;
sqlCommand.Parameters.Add("Age", SqlDbType.Int).Value = Age;
sqlCommand.Parameters.Add("Gender", SqlDbType.VarChar).Value =
Gender;
sqlCommand.Parameters.Add("Program", SqlDbType.VarChar).Value = Program;
sqlConnect.Open();
sqlCommand.ExecuteNonQuery();
sqlConnect.Close();
return true;
}
public bool UpdateStudent(long StudentID, int Age, string Program)
{
sqlCommand = new SqlCommand("UPDATE ClubMembers SET Age = @Age, Program
= @Program WHERE StudentId = @StudentId", sqlConnect);

sqlCommand.Parameters.AddWithValue("@StudentId", StudentID);
sqlCommand.Parameters.AddWithValue("@Age", Age);
sqlCommand.Parameters.AddWithValue("@Program", Program);
sqlConnect.Open();
sqlCommand.ExecuteNonQuery();
sqlConnect.Close();
return true;
}
public void DisplayID(ComboBox comboBox)
{
string getId = "SELECT * FROM ClubMembers";
sqlCommand = new SqlCommand(getId, sqlConnect);
sqlConnect.Open();
sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
comboBox.Items.Add(sqlReader["StudentId"]);

}
sqlConnect.Close();
}
public void DisplayText(string id)
{
string getId = "SELECT * FROM ClubMembers WHERE StudentId = @Id";
sqlCommand = new SqlCommand(getId, sqlConnect);
sqlCommand.Parameters.AddWithValue("@Id", id);
sqlConnect.Open();
sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
_FirstName = sqlReader.GetString(2);
_MiddleName = sqlReader.GetString(3);
_LastName = sqlReader.GetString(4);
_Age = sqlReader.GetInt32(5);
_Gender = sqlReader.GetString(6);
_Program = sqlReader.GetString(7);
}
sqlConnect.Close();
}
}
}

OUTPUT
Challenge Exercise:
1. In this challenge exercise, the program can do updates using the FrmUpdateMember.
2. The form must select the student ID first. See the image below. Clicking the Update Button

Once you have selected the student ID, it should display all the information in each field. Once click the
ID it will automatically show the all information of the students.
3. If the update is confirmed, the updates should be reflected in the FrmClubRegistration DataGridView.
As you can see I updated my age from 21 to 1 and my gender to male to female
As you can see in the data grid view my gender and age were already changed

You might also like