0% found this document useful (0 votes)
33 views6 pages

ABBAS EVENT 07LABexer1

The document contains C# code for a Windows Forms application that allows users to register, update, and view members of a club. It includes code to connect to a SQL database, insert, update, and select member data from tables, and populate dropdowns and datagrids on the form. Functions are used to load student IDs, register new members by inserting data, update existing members by updating columns, and refresh the datagrid by reselecting data from the database table.

Uploaded by

Saiden Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

ABBAS EVENT 07LABexer1

The document contains C# code for a Windows Forms application that allows users to register, update, and view members of a club. It includes code to connect to a SQL database, insert, update, and select member data from tables, and populate dropdowns and datagrids on the form. Functions are used to load student IDs, register new members by inserting data, update existing members by updating columns, and refresh the datagrid by reselecting data from the database table.

Uploaded by

Saiden Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Registration
{
public partial class FrmUpdateMembers : Form
{
private const string ConnectionString = "Data Source=DESKTOP-2GR1QNV\\SQLEXPRESS;Initial
Catalog=StudentList;Integrated Security=True";

public FrmUpdateMembers()
{
InitializeComponent();
LoadStudentIds();

}
private void LoadStudentIds()
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();

string query = "SELECT StudentId FROM ClubMember";


using (SqlCommand cmd = new SqlCommand(query, connection))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
cmbID.Items.Add(reader["StudentId"].ToString());
}
}
}
}
}

private void cmbStudentId_SelectedIndexChanged(object sender, EventArgs e)


{
string selectedStudentId = cmbID.SelectedItem.ToString();

using (SqlConnection connection = new SqlConnection(ConnectionString))


{
connection.Open();

string query = "SELECT * FROM ClubMember WHERE StudentId = @StudentId";


using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@StudentId", selectedStudentId);

using (SqlDataReader reader = cmd.ExecuteReader())


{
if (reader.Read())
{
txtFname.Text = reader["FirstName"].ToString();
txtMname.Text = reader["MiddleName"].ToString();
txtLname.Text = reader["LastName"].ToString();
txtAge.Text = reader["Age"].ToString();
cmbGender.SelectedItem = reader["Gender"].ToString();
cmbCourse.Text = reader["Program"].ToString();
}
}
}
}
}
private void cmbID_SelectedIndexChanged(object sender, EventArgs e)
{

private void button1_Click(object sender, EventArgs e)


{

private void FrmUpdateMembers_Load(object sender, EventArgs e)


{
string[] courses = new string[]
{
"BS Information Technology",
"BS Computer Science",
"BS Business Administration",
};
for (int x = 0; x < 3; x++)
{
cmbCourse.Items.Add(courses[x].ToString());
}
string[] Gender = new string[]
{
"Male",
"Female",
"Others"
};
for (int x = 0; x < 3; x++)
{
cmbGender.Items.Add(Gender[x].ToString());
}
}

private void cmbConfirm_Click(object sender, EventArgs e)


{

string selectedStudentId = cmbID.SelectedItem.ToString();

using (SqlConnection connection = new SqlConnection(ConnectionString))


{
connection.Open();

string query = "UPDATE ClubMember SET FirstName = @FirstName, MiddleName = @MiddleName, " +
"LastName = @LastName, Age = @Age, Gender = @Gender, Program = @Program " +
"WHERE StudentId = @StudentId";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@StudentId", selectedStudentId);
cmd.Parameters.AddWithValue("@FirstName", txtFname.Text);
cmd.Parameters.AddWithValue("@MiddleName", txtMname.Text);
cmd.Parameters.AddWithValue("@LastName", txtLname.Text);
cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Gender", cmbGender.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@Program", cmbCourse.Text);

cmd.ExecuteNonQuery();
}
cmbID.SelectedIndex = -1;
txtFname.Clear();
txtMname.Clear();
txtLname.Clear();
txtAge.Clear();
cmbGender.SelectedIndex = -1;
cmbCourse.SelectedIndex = -1;

MessageBox.Show("Update successful!");
}
}
}
}

form1
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Registration
{
public partial class FrmClubRegristration : Form
{
SqlConnection connect = new SqlConnection(@"Data Source=DESKTOP-2GR1QNV\SQLEXPRESS;Initial
Catalog=StudentList;Integrated Security=True");

public FrmClubRegristration()
{
InitializeComponent();
connect.Open();
}

private void btnUpdate_Click(object sender, EventArgs e)


{
FrmUpdateMembers UpdateMembers = new FrmUpdateMembers();
UpdateMembers.Show();

private void btnRegister_Click(object sender, EventArgs e)


{

if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtFname.Text) ||
string.IsNullOrEmpty(txtMname.Text) || string.IsNullOrEmpty(txtLname.Text) || string.IsNullOrEmpty(txtAge.Text) ||
string.IsNullOrEmpty(cmbGender.Text) || string.IsNullOrEmpty(cmbCourse.Text))
{
MessageBox.Show("Please fill in all the required fields.", "Validation Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;

try
{
string insertData = "INSERT INTO ClubMember(StudentId, FirstName, MiddleName, LastName, Age, Gender,
Program)" +
"VALUES( @StudentId, @FirstName, @MiddleName, @LastName, @Age, @Gender,
@Program)";

using (SqlCommand sqlCommand = new SqlCommand(insertData, connect))


{
sqlCommand.Parameters.AddWithValue("@StudentId", txtId.Text.Trim());
sqlCommand.Parameters.AddWithValue("@FirstName", txtFname.Text.Trim());
sqlCommand.Parameters.AddWithValue("@MiddleName", txtMname.Text.Trim());
sqlCommand.Parameters.AddWithValue("@LastName", txtLname.Text.Trim());
sqlCommand.Parameters.AddWithValue("@Age", txtAge.Text.Trim());
sqlCommand.Parameters.AddWithValue("@Gender", cmbGender.Text.Trim());
sqlCommand.Parameters.AddWithValue("@Program", cmbCourse.Text.Trim());

sqlCommand.ExecuteNonQuery();
MessageBox.Show("Registered successfully", "Information Message", MessageBoxButtons.OK,
MessageBoxIcon.Information);

txtId.Clear();
txtFname.Clear();
txtMname.Clear();
txtLname.Clear();
txtAge.Clear();
cmbGender.SelectedIndex = -1;
cmbCourse.SelectedIndex = -1;
}

using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-2GR1QNV\


SQLEXPRESS;Initial Catalog=StudentList;Integrated Security=True"))
{
connection.Open();

using (DataTable dt = new DataTable("ClubMember"))


{
using (SqlCommand cmd = new SqlCommand("SELECT StudentId, FirstName, MiddleName, LastName,
Age, Gender, Program FROM ClubMember", connection))
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
sqlDataAdapter.Fill(dt);

dataGridView1.DataSource = dt;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
private void FrmClubRegistration_Load(object sender, EventArgs e)
{
string[] courses = new string[]
{
"BS Information Technology",
"BS Computer Science",
"BS Business Administration",
};
for (int x = 0; x < 3; x++)
{
cmbCourse.Items.Add(courses[x].ToString());
}
string[] Gender = new string[]
{
"Male",
"Female",
"Others"
};
for (int x = 0; x < 3; x++)
{
cmbGender.Items.Add(Gender[x].ToString());
}

private void FrmClubRegristration_Load(object sender, EventArgs e)


{

private void btnRefresh_Click(object sender, EventArgs e)


{
try
{
using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-2GR1QNV\
SQLEXPRESS;Initial Catalog=StudentList;Integrated Security=True"))
{
connection.Open();

using (DataTable dt = new DataTable("ClubMember"))


{
using (SqlCommand cmd = new SqlCommand("SELECT StudentId, FirstName, MiddleName, LastName,
Age, Gender, Program FROM ClubMember", connection))
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
sqlDataAdapter.Fill(dt);

dataGridView1.DataSource = dt;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Displaying Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
}

You might also like