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.Threading.Tasks; using System.Windows.Forms; namespace GeoTema { public partial class InsertData : Form { public InsertData() { InitializeComponent(); } private void btnAddData_Click(object sender, EventArgs e) { string Continent1="", Continent2=""; string Country = txtCountry.Text; string Input1 = dpmContinent1.Text; string Input2 = dpmContinent2.Text; string Ranking = txtRanking.Text; string Birthrate = txtBirthrate.Text; int InvalidChar = 0; if (Country == "" || Input1 == "" || Ranking == "" || Birthrate == "") MessageBox.Show("Please fill in the necessary fields!"); else { string statement1 = "use fødselsrate_2017 select Land from Land where Land = @Exist"; if (Sql.ExistCheck(statement1, Country)) MessageBox.Show("Country already exists in the database!"); else { for (int i = 0; i < Ranking.Length; i++) { if ("0123456789".IndexOf(Ranking.Substring(i, 1)) < 0) InvalidChar++; } for (int i = 0; i < Birthrate.Length; i++) { if ("0123456789.,".IndexOf(Birthrate.Substring(i, 1)) < 0) InvalidChar++; } string[] BirthArray = Birthrate.Split(new char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries); if (InvalidChar > 0 || BirthArray.Length == 0 || BirthArray.Length > 2) MessageBox.Show("Incorrect format in Ranking or Birthrate!"); else { if (BirthArray.Length == 2) Birthrate = BirthArray[0] + "." + BirthArray[1]; else if (BirthArray.Length == 1) Birthrate = BirthArray[0] + ".0"; switch (Input1) { case "Europe": Continent1 = "1"; break; case "North America": Continent1 = "2"; break; case "South America": Continent1 = "3"; break; case "Oceania": Continent1 = "4"; break; case "Asia": Continent1 = "5"; break; case "Africa": Continent1 = "6"; break; case "Antarctica": Continent1 = "7"; break; } switch (Input2) { case "Europe": Continent2 = "1"; break; case "North America": Continent2 = "2"; break; case "South America": Continent2 = "3"; break; case "Oceania": Continent2 = "4"; break; case "Asia": Continent2 = "5"; break; case "Africa": Continent2 = "6"; break; case "Antarctica": Continent2 = "7"; break; case "": case "": Continent2 = "null"; break; } try { List param = new List { Country, Ranking, Birthrate }; string statement2 = "use fødselsrate_2017 insert into Land values(@Country," + Continent1 + "," + Continent2 + ") insert into Rang values (@Rank, @Birth)"; Sql.SQLConnect(statement2, param); MessageBox.Show("Data added to database!"); } catch (Exception m) { MessageBox.Show(m.Message); } } } } } private void btnBack_Click(object sender, EventArgs e) { switch (MainMenu.type) { case "administrator": { this.Hide(); AdminMenu Admin1 = new AdminMenu(); Admin1.ShowDialog(); Close(); break; } case "super user": { this.Hide(); SuperMenu Super1 = new SuperMenu(); Super1.ShowDialog(); Close(); break; } } } } }