[MySQL Student App] Finito

This commit is contained in:
Alex Thomassen 2019-04-11 14:07:29 +02:00
parent a82ba61675
commit fc275c2f5e
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE
7 changed files with 315 additions and 23 deletions

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQLStoredProcedureStudent
{
public class FylkeItem
{
public int Id { get; set; }
public string Name { get; set; }
public FylkeItem (int id, string name)
{
Id = id;
Name = name;
}
public override string ToString()
{
return Name;
}
}
}

View File

@ -53,6 +53,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FylkeItem.cs" />
<Compile Include="Poststed.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -35,17 +35,20 @@
this.gridPoststedList = new System.Windows.Forms.DataGridView();
this.txtPoststed = new System.Windows.Forms.TextBox();
this.lblPoststed = new System.Windows.Forms.Label();
this.listFylke = new System.Windows.Forms.ComboBox();
this.lblFylke = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.gridPoststedList)).BeginInit();
this.SuspendLayout();
//
// btnPoststedAdd
//
this.btnPoststedAdd.Location = new System.Drawing.Point(81, 227);
this.btnPoststedAdd.Location = new System.Drawing.Point(81, 242);
this.btnPoststedAdd.Name = "btnPoststedAdd";
this.btnPoststedAdd.Size = new System.Drawing.Size(166, 23);
this.btnPoststedAdd.TabIndex = 14;
this.btnPoststedAdd.TabIndex = 4;
this.btnPoststedAdd.Text = "Legg til poststed";
this.btnPoststedAdd.UseVisualStyleBackColor = true;
this.btnPoststedAdd.Click += new System.EventHandler(this.BtnPoststedAdd_Click);
//
// lblPostnummer
//
@ -87,11 +90,30 @@
this.lblPoststed.TabIndex = 9;
this.lblPoststed.Text = "Poststed:";
//
// listFylke
//
this.listFylke.FormattingEnabled = true;
this.listFylke.Location = new System.Drawing.Point(81, 215);
this.listFylke.Name = "listFylke";
this.listFylke.Size = new System.Drawing.Size(166, 21);
this.listFylke.TabIndex = 3;
//
// lblFylke
//
this.lblFylke.AutoSize = true;
this.lblFylke.Location = new System.Drawing.Point(7, 218);
this.lblFylke.Name = "lblFylke";
this.lblFylke.Size = new System.Drawing.Size(35, 13);
this.lblFylke.TabIndex = 16;
this.lblFylke.Text = "Fylke:";
//
// Poststed
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.lblFylke);
this.Controls.Add(this.listFylke);
this.Controls.Add(this.btnPoststedAdd);
this.Controls.Add(this.lblPostnummer);
this.Controls.Add(this.txtPostnummer);
@ -117,5 +139,7 @@
private System.Windows.Forms.DataGridView gridPoststedList;
private System.Windows.Forms.TextBox txtPoststed;
private System.Windows.Forms.Label lblPoststed;
private System.Windows.Forms.ComboBox listFylke;
private System.Windows.Forms.Label lblFylke;
}
}

View File

@ -21,7 +21,90 @@ namespace MySQLStoredProcedureStudent
{
InitializeComponent();
_parentForm = parentForm;
_connection = connection;
ClearFields();
GridFill();
UpdateFylkeList();
}
private void ClearFields()
{
txtPostnummer.Clear();
txtPoststed.Clear();
}
private void GridFill()
{
var command = new MySqlDataAdapter("PoststedViewAll", _connection);
command.SelectCommand.CommandType = CommandType.StoredProcedure;
var dataTable = new DataTable();
command.Fill(dataTable);
gridPoststedList.DataSource = dataTable;
// Zerofill "Postnummer"
gridPoststedList.Columns[0].DefaultCellStyle.Format = "D4";
// Hide Fylke IDs.
gridPoststedList.Columns[2].Visible = false;
gridPoststedList.Columns[3].Visible = false;
gridPoststedList.Columns[4].HeaderText = "Fylke";
}
private void UpdateFylkeList()
{
listFylke.Items.Clear();
var command = new MySqlDataAdapter("FylkeViewAll", _connection);
command.SelectCommand.CommandType = CommandType.StoredProcedure;
var dataTable = new DataTable();
command.Fill(dataTable);
foreach (DataRow row in dataTable.Rows)
{
var items = row.ItemArray;
var id = int.Parse(items[0].ToString());
var name = items[1].ToString();
var fylkeItem = new FylkeItem(id, name);
listFylke.Items.Add(fylkeItem);
}
}
private void BtnPoststedAdd_Click(object sender, EventArgs e)
{
try
{
var command = new MySqlCommand("PoststedAdd", _connection);
command.CommandType = CommandType.StoredProcedure;
var postnummer = Convert.ToInt32(txtPostnummer.Text.Trim());
var poststed = txtPoststed.Text.Trim();
var fylke = (FylkeItem) listFylke.SelectedItem;
command.Parameters.AddWithValue("_Postnummer", postnummer);
command.Parameters.AddWithValue("_Poststed", poststed);
command.Parameters.AddWithValue("_FylkeId", fylke.Id);
command.ExecuteNonQuery();
MessageBox.Show($"Poststed er lagret: {postnummer} {poststed}");
GridFill();
ClearFields();
_parentForm.UpdatePoststedList();
}
catch (Exception ex)
{
MessageBox.Show("Feil har oppstått ved lagring av poststed.");
MessageBox.Show(ex.ToString());
}
}
}
}

View File

@ -8,12 +8,18 @@ namespace MySQLStoredProcedureStudent
{
public class PoststedItem
{
public int PostnummerInt { get; set; }
public string Poststed { get; set; }
public string Postnummer { get; set; }
public PoststedItem(string postnummer, string poststed)
public PoststedItem(int postnummer, string poststed)
{
Postnummer = postnummer;
// In the database I have "ZEROFILL", but when retrieving
// this value it will not be zerofilled. This is a dirty hack
// so that the visual representation is as it should be in the list.
Postnummer = (postnummer.ToString()).PadLeft(4, '0');
PostnummerInt = postnummer;
Poststed = poststed;
}

View File

@ -48,7 +48,7 @@
this.lblAge = new System.Windows.Forms.Label();
this.btnOpenPoststed = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.listGender = new System.Windows.Forms.ListBox();
this.numAge = new System.Windows.Forms.NumericUpDown();
this.listPoststed = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.gridStudentList)).BeginInit();
@ -62,6 +62,7 @@
this.gridStudentList.Name = "gridStudentList";
this.gridStudentList.Size = new System.Drawing.Size(892, 520);
this.gridStudentList.TabIndex = 0;
this.gridStudentList.DoubleClick += new System.EventHandler(this.GridStudentList_DoubleClick);
//
// txtStudentSearch
//
@ -88,6 +89,7 @@
this.btnSearchStudent.TabIndex = 13;
this.btnSearchStudent.Text = "Søk";
this.btnSearchStudent.UseVisualStyleBackColor = true;
this.btnSearchStudent.Click += new System.EventHandler(this.BtnSearchStudent_Click);
//
// btnSaveStudent
//
@ -98,6 +100,7 @@
this.btnSaveStudent.TabIndex = 9;
this.btnSaveStudent.Text = "Lagre";
this.btnSaveStudent.UseVisualStyleBackColor = true;
this.btnSaveStudent.Click += new System.EventHandler(this.BtnSaveStudent_Click);
//
// btnDeleteStudent
//
@ -108,6 +111,7 @@
this.btnDeleteStudent.TabIndex = 10;
this.btnDeleteStudent.Text = "Slett";
this.btnDeleteStudent.UseVisualStyleBackColor = true;
this.btnDeleteStudent.Click += new System.EventHandler(this.BtnDeleteStudent_Click);
//
// btnCancelStudent
//
@ -118,6 +122,7 @@
this.btnCancelStudent.TabIndex = 11;
this.btnCancelStudent.Text = "Avbryt";
this.btnCancelStudent.UseVisualStyleBackColor = true;
this.btnCancelStudent.Click += new System.EventHandler(this.BtnCancelStudent_Click);
//
// txtFirstName
//
@ -221,16 +226,16 @@
this.label1.TabIndex = 25;
this.label1.Text = "Kjønn (M/K):";
//
// listBox1
// listGender
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Items.AddRange(new object[] {
this.listGender.FormattingEnabled = true;
this.listGender.Items.AddRange(new object[] {
"M",
"K"});
this.listBox1.Location = new System.Drawing.Point(104, 197);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(313, 30);
this.listBox1.TabIndex = 8;
this.listGender.Location = new System.Drawing.Point(104, 197);
this.listGender.Name = "listGender";
this.listGender.Size = new System.Drawing.Size(313, 30);
this.listGender.TabIndex = 8;
//
// numAge
//
@ -269,7 +274,7 @@
this.ClientSize = new System.Drawing.Size(1330, 565);
this.Controls.Add(this.listPoststed);
this.Controls.Add(this.numAge);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.listGender);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnOpenPoststed);
this.Controls.Add(this.lblAge);
@ -321,7 +326,7 @@
private System.Windows.Forms.Label lblAge;
private System.Windows.Forms.Button btnOpenPoststed;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listGender;
private System.Windows.Forms.NumericUpDown numAge;
private System.Windows.Forms.ComboBox listPoststed;
}

View File

@ -45,12 +45,7 @@ namespace MySQLStoredProcedureStudent
{
var items = row.ItemArray;
var postnummer = items[0].ToString();
// In the database I have "ZEROFILL", but when retrieving
// this value it will not be zerofilled. This is a dirty hack
// so that the visual representation is as it should be in the list.
postnummer = postnummer.PadLeft(4, '0');
var postnummer = int.Parse(items[0].ToString());
var poststed = items[1].ToString();
var poststedItem = new PoststedItem(postnummer, poststed);
@ -58,17 +53,23 @@ namespace MySQLStoredProcedureStudent
}
}
private void GridFill()
private void GridFill(MySqlDataAdapter command = null)
{
var command = new MySqlDataAdapter("StudentViewAll", _connection);
command.SelectCommand.CommandType = CommandType.StoredProcedure;
if (command == null)
{
command = new MySqlDataAdapter("StudentViewAll", _connection);
command.SelectCommand.CommandType = CommandType.StoredProcedure;
}
var dataTable = new DataTable();
command.Fill(dataTable);
gridStudentList.DataSource = dataTable;
// Hide "idStudent" column
gridStudentList.Columns[0].Visible = false;
// Format "Postnummer" column
gridStudentList.Columns[7].DefaultCellStyle.Format = "D4";
}
private void ClearFields()
@ -79,6 +80,9 @@ namespace MySQLStoredProcedureStudent
txtPhone.Clear();
numAge.Value = 20;
listPoststed.SelectedIndex = -1;
listGender.SelectedIndex = -1;
_studentId = 0;
btnSaveStudent.Text = "Lagre";
@ -94,5 +98,150 @@ namespace MySQLStoredProcedureStudent
poststedForm.Show();
}
private void BtnSaveStudent_Click(object sender, EventArgs e)
{
try
{
var command = new MySqlCommand("StudentAddOrEdit", _connection);
command.CommandType = CommandType.StoredProcedure;
var firstName = txtFirstName.Text.Trim();
var lastName = txtLastName.Text.Trim();
var address = txtAddress.Text.Trim();
var poststed = (PoststedItem)listPoststed.SelectedItem;
var phone = txtPhone.Text.Trim();
var age = numAge.Value;
var gender = listGender.Text.Trim();
command.Parameters.AddWithValue("_Id", _studentId);
command.Parameters.AddWithValue("_Fornavn", firstName);
command.Parameters.AddWithValue("_Etternavn", lastName);
command.Parameters.AddWithValue("_Adresse", address);
command.Parameters.AddWithValue("_Telefon", phone);
command.Parameters.AddWithValue("_Alder", age);
command.Parameters.AddWithValue("_Kjonn", gender);
command.Parameters.AddWithValue("_Postnummer", poststed.PostnummerInt);
command.ExecuteNonQuery();
MessageBox.Show($"Student er lagret: {firstName} {lastName}");
GridFill();
ClearFields();
}
catch (Exception ex)
{
MessageBox.Show("Feil har oppstått ved oppdatering av student.");
MessageBox.Show(ex.ToString());
}
}
private void GridStudentList_DoubleClick(object sender, EventArgs e)
{
var row = gridStudentList.CurrentRow;
if (row != null && row.Index > -1)
{
try
{
var cells = row.Cells;
var count = cells.Count;
var values = new string[count];
for (int i = 0; i < count; i++)
{
values[i] = cells[i].Value.ToString();
}
txtFirstName.Text = values[1];
txtLastName.Text = values[2];
txtAddress.Text = values[3];
txtPhone.Text = values[4];
numAge.Value = Convert.ToInt32(values[5]);
var gender = values[6];
if (gender == "M")
{
listGender.SelectedIndex = 0;
}
// gender == "M"
else
{
listGender.SelectedIndex = 1;
}
var postnummer = Convert.ToInt32(values[7]);
var poststed = listPoststed.Items.Cast<PoststedItem>().FirstOrDefault(x => x.PostnummerInt == postnummer);
var poststedIndex = listPoststed.Items.IndexOf(poststed);
listPoststed.SelectedIndex = poststedIndex;
_studentId = Convert.ToInt32(values[0]);
btnSaveStudent.Text = "Oppdater";
btnDeleteStudent.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("En feil oppstod ved valg av student.");
MessageBox.Show(ex.ToString());
}
}
}
private void BtnCancelStudent_Click(object sender, EventArgs e)
{
ClearFields();
}
private void BtnSearchStudent_Click(object sender, EventArgs e)
{
try
{
var searchText = txtStudentSearch.Text.Trim();
if (string.IsNullOrEmpty(searchText))
{
GridFill();
return;
}
var command = new MySqlDataAdapter("StudentSearchByText", _connection);
command.SelectCommand.CommandType = CommandType.StoredProcedure;
command.SelectCommand.Parameters.AddWithValue("_SearchText", searchText);
GridFill(command);
}
catch (Exception ex)
{
MessageBox.Show("Feil har oppstått ved søk.");
MessageBox.Show(ex.ToString());
}
}
private void BtnDeleteStudent_Click(object sender, EventArgs e)
{
if (_studentId > 0)
{
try
{
var command = new MySqlCommand("StudentDeleteById", _connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("_StudentId", _studentId);
command.ExecuteNonQuery();
var name = $"{txtFirstName.Text.Trim()} {txtLastName.Text.Trim()}";
MessageBox.Show($"Slettet student: {name}");
GridFill();
ClearFields();
}
catch (Exception ex)
{
MessageBox.Show("Feil har oppstått ved sletting av student.");
MessageBox.Show(ex.ToString());
}
}
}
}
}