using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.Properties; using Point_Of_Sale_Managment; 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 BiskLog_Point_Of_Sale.Company_Setup { public partial class DatabaseLocation : Form { Holder holder; SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); bool companyCreated = false; public static string company; public DatabaseLocation(Holder hol) { InitializeComponent(); holder = hol; } private void Button2_Click(object sender, EventArgs e) { holder.moveToFirst(); } private void BranchSet_CheckedChanged(object sender, EventArgs e) { if (branchSet.Checked) { button1.Text = "Set up new branch"; } else if (!branchSet.Checked) { button1.Text = "LOGIN"; } } private async void Button1_Click(object sender, EventArgs e) { if (password.Text.Equals(passwordConfirmation.Text)) { if (!String.IsNullOrEmpty(serverLocation.Text) && !String.IsNullOrEmpty(password.Text) && !String.IsNullOrEmpty(txtusername.Text)) { if (branchSet.Checked) { Task task = new Task(() => { try { serverLocation.Invoke(new Action(() => { if (!String.IsNullOrEmpty(portNumber.Text)) { Settings.Default["Connection"] = passwordEncryption.Encrypt("Data Source=" + serverLocation.Text + "," + portNumber.Text + ";Database=BISK_POS;User ID= " + txtusername.Text + ";Password=" + password.Text); } else { Settings.Default["Connection"] = passwordEncryption.Encrypt("Data Source=" + serverLocation.Text +";Database=BISK_POS;User ID= " + txtusername.Text + ";Password=" + password.Text); } Settings.Default.Save(); cn = new SqlConnection(conn.MyConnection()); cn.Open(); cm = new SqlCommand("Select company_name from tblCompanyDetails", cn); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows && !String.IsNullOrEmpty(dr[0].ToString())) { company = dr[0].ToString(); companyCreated = true; } dr.Close(); cn.Close(); })); return 1; } catch { cn.Close(); return 2; } }); holdup.Visible = true; button1.Enabled = false; button2.Enabled = false; task.Start(); int result = await task; if (result == 1) { if (companyCreated) { holder.moveToFinalBranch(); } else { string title = "Company not set up"; string message = "Company hasn't been setup properly, navigate to the previous page to properly set up a new company"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } else { cn.Close(); string title = "Unable to connect to server"; string message = "Sorry we are unable to connect to the company's database at this time, please verify the " + "credentials provided are correct and try again !!"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } else { Task task = new Task(() => { try { serverLocation.Invoke(new Action(() => { if (!String.IsNullOrEmpty(portNumber.Text)) { Settings.Default["Connection"] = passwordEncryption.Encrypt("Data Source=" + serverLocation.Text + "," + portNumber.Text + ";Database=BISK_POS;User ID= " + txtusername.Text + ";Password=" + password.Text); } else { Settings.Default["Connection"] = passwordEncryption.Encrypt("Data Source=" + serverLocation.Text + ";Database=BISK_POS;User ID= " + txtusername.Text + ";Password=" + password.Text); } Settings.Default.Save(); cn = new SqlConnection(conn.MyConnection()); cn.Open(); cm = new SqlCommand("Select company_name from tblCompanyDetails", cn); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows && !String.IsNullOrEmpty(dr[0].ToString())) { company = dr[0].ToString(); companyCreated = true; } dr.Close(); cn.Close(); })); return 1; } catch (Exception ex) { Console.WriteLine(ex.ToString()); cn.Close(); return 2; } }); holdup.Visible = true; button1.Enabled = false; button2.Enabled = false; task.Start(); int result = await task; if (result == 1) { if (companyCreated) { SelectBranch selectBranch = new SelectBranch(); selectBranch.BringToFront(); selectBranch.ShowDialog(); if (!String.IsNullOrEmpty(Settings.Default.BranchID)) { holder.moveToLogin(); } else { string title = "Select Branch"; string message = "To be able to login, you need to first select your branch"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } else { string title = "Company not set up"; string message = "Company hasn't been setup properly, navigate to the previous page to properly set up a new company"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } else { string title = "Unable to connect to server"; string message = "Sorry we are unable to connect to the company's database at this time, please verify the " + "credentials provided are correct and try again !!"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } holdup.Visible = false; button1.Enabled = true; button2.Enabled = true; } } else { a1.Visible = true; a2.Visible = true; a3.Visible = true; a4.Visible = true; string title = "Essential fields empty"; string message = "Fields with asterisk have to be filled to continue"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } else { string title = "Passwords do not match"; string message = "Passwords do not match, make sure they match to be able to continue"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } } }