private void ConfirmBtn_Click(object sender, EventArgs e) { Supplier InsightSupplier = new Supplier(); ContactPerson InsightCP = new ContactPerson(); Address InsightAddress = new Address(); InsightSupplier.SupplierName = SupName; InsightAddress.AddressDescription = AddL1 + " " + AddL2 + " " + AddL3; //InsightAddress.City = City; //InsightAddress.County = Country; InsightCP.ContactPersonName = CPName; InsightCP.ContactPersonPhoneNumber = Convert.ToInt32(CPNumber); InsightCP.ContactPersonJobDescription = JobD; InsightCP.ContactPersonEmailAddress = CPEmail; InsightCP.SupplierID = InsightSupplier.SupplierID; InsightSupplier.AddressID = InsightAddress.AddressID; using (InsightEntities db = new InsightEntities()) { db.Suppliers.Add(InsightSupplier); db.ContactPersons.Add(InsightCP); db.Addresses.Add(InsightAddress); db.SaveChanges(); } }
private void button13_Click(object sender, EventArgs e) { string clientAddress = addressline1.Text + ", " + addressline2.Text + ", " + addressline3.Text; Client InsightClient = new Client(); Individual InsightIndividual = new Individual(); Address InsightClientAddress = new Address(); ClientLogin InsightClientLogin = new ClientLogin(); //Client Table InsightClient.ClientName = clientNametxt.Text; InsightClient.ClientNumber = Convert.ToInt32(clientNumbertxt.Text); //Address Table InsightClientAddress.AddressDescription = clientAddress; InsightClientAddress.CityID = Convert.ToInt32(citycbx.SelectedValue); #region store client and address info using (InsightEntities db = new InsightEntities()) { db.Addresses.Add(InsightClientAddress); db.SaveChanges(); } int clientTypeID = Convert.ToInt32(clientcbx.SelectedValue); int addressID = InsightClientAddress.AddressID; using (InsightEntities db = new InsightEntities()) { db.Clients.Add(InsightClient); InsightClient.ClientTypeID = clientTypeID; InsightClient.AddressID = addressID; db.SaveChanges(); } #endregion int clientID = InsightClient.ClientID; #region Individual if (clientcbx.Text == "Individual") { SqlConnection Myconn = new SqlConnection(globalClass.myConn); Myconn.Open(); SqlCommand insertIndividual = new SqlCommand("Insert Into Individual(ClientID, IndividualEmailAddress, IndividualDateOfBirth) Values (@ClientID, @IndividualEmailAddress, @IndividualDateOfBirth)", Myconn); insertIndividual.Parameters.AddWithValue("@ClientID", clientID); insertIndividual.Parameters.AddWithValue("@IndividualEmailAddress", contactEmail.Text); insertIndividual.Parameters.AddWithValue("@IndividualDateOfBirth", clientDOB.Value.Date); insertIndividual.ExecuteNonQuery(); Myconn.Close(); } #endregion #region Organisation if (clientcbx.Text == "Organisation") { ContactPerson organisationContact = new ContactPerson(); SqlConnection Myconn = new SqlConnection(globalClass.myConn); Myconn.Open(); SqlCommand insertOrganisation = new SqlCommand("Insert into Organisation(ClientID, OrganisationTypeID) Values (@ClientID, @OrganisationTypeID)", Myconn); insertOrganisation.Parameters.AddWithValue("@ClientID", clientID); insertOrganisation.Parameters.AddWithValue("@OrganisationTypeID", organisationTypecbx.SelectedValue); insertOrganisation.ExecuteNonQuery(); Myconn.Close(); organisationContact.ContactPersonEmailAddress = contactEmail.Text; organisationContact.ContactPersonJobDescription = contactJobtxt.Text; organisationContact.ContactPersonName = contactNametxt.Text; organisationContact.ContactPersonPhoneNumber = Convert.ToInt32(contactNumbertxt.Text); organisationContact.ClientID = clientID; using (InsightEntities db = new InsightEntities()) { db.ContactPersons.Add(organisationContact); db.SaveChanges(); } } #endregion string username = Char.ToUpper(clientcbx.Text[0]) + "." + clientNamelbl.Text; #region generate username and password SqlConnection conn = new SqlConnection(globalClass.myConn); SqlDataReader myReader; int usernumber = 1; int tmp = 0; try { while (tmp == 0) { SqlCommand checkUser = new SqlCommand("Select ClientUsername From ClientLogin Where ClientUsername ="******"'" + username + "'", conn); conn.Open(); myReader = checkUser.ExecuteReader(); if (myReader.HasRows) { username = username + Convert.ToString(usernumber); usernumber++; } else { myReader.Close(); conn.Close(); tmp = 1; } } } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } string pass = username + "#123"; //Generate hash HashAlgorithm hashFunc = SHA256.Create(); byte[] hold = hashFunc.ComputeHash(Encoding.UTF8.GetBytes(pass)); StringBuilder hashString = new StringBuilder(); foreach (byte b in hold) { hashString.Append(b.ToString("X2")); } //Store hash string in database InsightClientLogin.ClientPassword = hashString.ToString(); InsightClientLogin.AccessLevelID = 2; #endregion using (InsightEntities db = new InsightEntities()) { db.ClientLogins.Add(InsightClientLogin); db.SaveChanges(); } //email feature MessageBox.Show("Successful, Username: "******" Password: " + pass); }