0% found this document useful (0 votes)
16 views7 pages

StudentsForm Cs

The document contains a C# Windows Forms application for managing students in a database. It includes functionality to load students from a SQL database and add new students through a user interface. The application handles database connections and exceptions while providing feedback to the user.

Uploaded by

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

StudentsForm Cs

The document contains a C# Windows Forms application for managing students in a database. It includes functionality to load students from a SQL database and add new students through a user interface. The application handles database connections and exceptions while providing feedback to the user.

Uploaded by

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

using System;

using System.Data.SqlClient;

using System.Windows.Forms;

namespace ScolariteApp

public partial class StudentsForm : Form

private string connectionString = "Server=.;Database=ScolariteDB;Integrated Security=True;";

private SqlConnection connection;

public StudentsForm()

InitializeComponent();

connection = new SqlConnection(connectionString);

LoadStudents();

private void LoadStudents()

try

connection.Open();

string query = "SELECT * FROM Students";

SqlCommand command = new SqlCommand(query, connection);


SqlDataReader reader = command.ExecuteReader();

listBoxStudents.Items.Clear();

while (reader.Read())

string studentInfo = $"ID: {reader["StudentID"]}, Name: {reader["Name"]}, Grade:


{reader["Grade"]}";

listBoxStudents.Items.Add(studentInfo);

reader.Close();

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally

connection.Close();

private void btnAddStudent_Click(object sender, EventArgs e)

string name = txtName.Text;

string grade = txtGrade.Text;


if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(grade))

MessageBox.Show("Please enter name and grade.");

return;

try

connection.Open();

string query = "INSERT INTO Students (Name, Grade) VALUES (@Name, @Grade)";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Name", name);

command.Parameters.AddWithValue("@Grade", grade);

command.ExecuteNonQuery();

MessageBox.Show("Student added successfully!");

txtName.Clear();

txtGrade.Clear();

LoadStudents();

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally
{

connection.Close();

private void InitializeComponent()

this.listBoxStudents = new System.Windows.Forms.ListBox();

this.txtName = new System.Windows.Forms.TextBox();

this.txtGrade = new System.Windows.Forms.TextBox();

this.btnAddStudent = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// listBoxStudents

//

this.listBoxStudents.FormattingEnabled = true;

this.listBoxStudents.Location = new System.Drawing.Point(20, 20);

this.listBoxStudents.Name = "listBoxStudents";

this.listBoxStudents.Size = new System.Drawing.Size(300, 160);

this.listBoxStudents.TabIndex = 0;

//

// txtName

//
this.txtName.Location = new System.Drawing.Point(120, 200);

this.txtName.Name = "txtName";

this.txtName.Size = new System.Drawing.Size(150, 20);

this.txtName.TabIndex = 1;

//

// txtGrade

//

this.txtGrade.Location = new System.Drawing.Point(120, 230);

this.txtGrade.Name = "txtGrade";

this.txtGrade.Size = new System.Drawing.Size(150, 20);

this.txtGrade.TabIndex = 2;

//

// btnAddStudent

//

this.btnAddStudent.Location = new System.Drawing.Point(150, 260);

this.btnAddStudent.Name = "btnAddStudent";

this.btnAddStudent.Size = new System.Drawing.Size(100, 30);

this.btnAddStudent.TabIndex = 3;

this.btnAddStudent.Text = "Add Student";

this.btnAddStudent.UseVisualStyleBackColor = true;

this.btnAddStudent.Click += new System.EventHandler(this.btnAddStudent_Click);

//

// label1

//

this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(50, 203);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(35, 13);

this.label1.TabIndex = 4;

this.label1.Text = "Name";

//

// label2

//

this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(50, 233);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(36, 13);

this.label2.TabIndex = 5;

this.label2.Text = "Grade";

//

// StudentsForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(350, 320);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.btnAddStudent);

this.Controls.Add(this.txtGrade);

this.Controls.Add(this.txtName);
this.Controls.Add(this.listBoxStudents);

this.Name = "StudentsForm";

this.Text = "Manage Students";

this.ResumeLayout(false);

this.PerformLayout();

private System.Windows.Forms.ListBox listBoxStudents;

private System.Windows.Forms.TextBox txtName;

private System.Windows.Forms.TextBox txtGrade;

private System.Windows.Forms.Button btnAddStudent;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

You might also like