private void button2_Click(object sender, EventArgs e) { if (radioButton1.Checked) { if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox5.Text != "" && textBox6.Text != "") { CompanyAccount Company = new CompanyAccount(); Company.Username = account.Username; Company.Password = account.Password; Company.CompanyName = textBox1.Text; Company.CompanyID = textBox2.Text; Company.EmailAddress = textBox3.Text; Company.FAX = textBox4.Text; Company.PhoneNumber = textBox5.Text; Company.Address1 = textBox6.Text; Company.Address2 = textBox7.Text; this.UseWaitCursor = true; button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; Task T = new Task(() => Da.RegisterCompanyAccount(Company)); T.Start(); } else { MessageBox.Show("Some fields are left empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else if (radioButton2.Checked) { if (textBox1.Text != "" && textBox2.Text != "" && textBox5.Text != "" && textBox4.Text != "") { SoloAccount Person = new SoloAccount(); Person.Username = account.Username; Person.Password = account.Password; Person.Name = textBox1.Text; Person.Surname = textBox2.Text; Person.DateOfBirth = dateTimePicker.Value; Person.EmailAddress = textBox4.Text; Person.PhoneNumber = textBox5.Text; this.UseWaitCursor = true; button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; Task T = new Task(() => Da.RegisterSoloAccount(Person)); T.Start(); } else { MessageBox.Show("Mandatory fields are empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
public void RegisterSoloAccount(SoloAccount Person) { try { string query1 = "Insert into Account (Username, Password, IsAdmin, Person_idClient) values (@Username, @Password, @IsAdmin, @Id);"; string query2 = "Insert into Person (Name, Surname, DateOfBirth, PhoneNumber, EmailAddress) values (@Name, @Surname, @Date, @Phone, @Email);"; string query = "select idClient from Person where Name = @Name and Surname = @Surname"; MySqlCommand comm = new MySqlCommand(query, connection); MySqlCommand command1 = new MySqlCommand(query1, connection); MySqlCommand command2 = new MySqlCommand(query2, connection); comm.Parameters.AddWithValue("@Name", Person.Name); comm.Parameters.AddWithValue("@Surname", Person.Surname); command2.Parameters.AddWithValue("@Name", Person.Name); command2.Parameters.AddWithValue("@Surname", Person.Surname); command2.Parameters.AddWithValue("@Date", Person.DateOfBirth.ToShortDateString()); command2.Parameters.AddWithValue("@Phone", Person.PhoneNumber); command2.Parameters.AddWithValue("@Email", Person.EmailAddress); OpenConnection(); command2.ExecuteNonQuery(); MySqlDataReader reader = comm.ExecuteReader(); reader.Read(); int Id = Convert.ToInt32(reader["idClient"]); reader.Close(); command1.Parameters.AddWithValue("@Id", Id); command1.Parameters.AddWithValue("@Username", Person.Username); command1.Parameters.AddWithValue("@Password", Person.Password); command1.Parameters.AddWithValue("@IsAdmin", 0); command1.ExecuteNonQuery(); CloseConnection(); MessageBox.Show("Registration successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); RegResponder(); Application.Run(new LogInForm()); } catch (Exception Ex) { MessageBox.Show(Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }