public int addCustomer(Customer newCustomer) { string name=newCustomer.Name; string phone=newCustomer.Phone; string email=newCustomer.Email; string filename2="customerDebt.db"; string myConnString="Data Source=" + filename2 + ";Version=3;"; string mySelectQuery = "insert into customers(name,phone,email) values(@name,@phone,@email);"+"Select last_insert_rowid();"; SQLiteConnection sqConnection = new SQLiteConnection(myConnString); SQLiteCommand sqCommand = new SQLiteCommand(mySelectQuery,sqConnection); sqCommand.CommandText=mySelectQuery; sqCommand.Parameters.AddWithValue("@name",name); sqCommand.Parameters.AddWithValue("@email",email); sqCommand.Parameters.AddWithValue("@phone",phone); sqConnection.Open(); Console.WriteLine("add customer ran"); int returnInt=Convert.ToInt32(sqCommand.ExecuteScalar()) ; sqConnection.Close(); return returnInt; }
void CustomerSaveClick(object sender, EventArgs e) { //check if fields are filled out if(this.customerName.MaskCompleted && this.customerName.Text.Trim() != string.Empty && this.customerEmail.MaskCompleted && checkIfEmail(this.customerEmail.Text.Trim()) && this.customerPhone.MaskCompleted) { //all fields are filled out if(this.CustomerIndex<0) { //new customer Customer newCustomer=new Customer(UppercaseWords(this.customerName.Text.Trim()),this.customerEmail.Text.Trim(),this.customerPhone.Text.Trim()); int returnedInt=MainForm.db.addCustomer(newCustomer); //MainForm.customerBL.Add(new KeyValuePair<int, string>(returnedInt, newCustomer.Name)); this.CustomerIndex=returnedInt; ResetCustomerList( new MainForm(true)); this.Close(); } else { //current customer..update Customer editCustomer=new Customer(this.CustomerIndex,UppercaseWords(this.customerName.Text.Trim()),this.customerEmail.Text.Trim(),this.customerPhone.Text.Trim()); MainForm.db.editCustomer(editCustomer); MainForm myMainForm=(MainForm)Application.OpenForms["MainForm"]; //ResetCustomerList( new MainForm(true),this.CustomerListIndex); ResetCustomerList( myMainForm,this.CustomerListIndex); this.Close(); } }else{ //fields missing MessageBox.Show("Please check input and retry.","Customer Input Error"); this.customerName.Focus(); return; } }
public Customer getCustomer(int index) { Customer returnCustomer=null; string filename2="customerDebt.db"; string myConnString="Data Source=" + filename2 + ";Version=3;"; string mySelectQuery = "select * from customers where id=@id"; SQLiteConnection sqConnection = new SQLiteConnection(myConnString); SQLiteCommand sqCommand = new SQLiteCommand(mySelectQuery,sqConnection); sqCommand.CommandText=mySelectQuery; sqCommand.Parameters.AddWithValue("@id",index); sqConnection.Open(); //Console.WriteLine("get customer ran"); SQLiteDataReader sqReader = sqCommand.ExecuteReader(); try { if (sqReader.Read()) { // Console.WriteLine(sqReader.GetInt32(0) + ", " + sqReader.GetString(1)); // Console.WriteLine(sqReader["name"] + ", " + sqReader.GetInt32(0)+sqReader["address"]); returnCustomer=new Customer(index,sqReader["name"].ToString(),sqReader["email"].ToString(),sqReader["phone"].ToString()); } } finally { // always call Close when done reading. sqReader.Close(); // always call Close when done reading. sqConnection.Close(); } return returnCustomer; }
public void editCustomer(Customer editCustomer) { int id=editCustomer.ID; string name=editCustomer.Name; string phone=editCustomer.Phone; string email=editCustomer.Email; string filename2="customerDebt.db"; string myConnString="Data Source=" + filename2 + ";Version=3;"; string mySelectQuery = "update customers set name=@name,phone=@phone,email=@email where id=@id"; SQLiteConnection sqConnection = new SQLiteConnection(myConnString); SQLiteCommand sqCommand = new SQLiteCommand(mySelectQuery,sqConnection); sqCommand.CommandText=mySelectQuery; sqCommand.Parameters.AddWithValue("@id",id); sqCommand.Parameters.AddWithValue("@name",name); sqCommand.Parameters.AddWithValue("@email",email); sqCommand.Parameters.AddWithValue("@phone",phone); sqConnection.Open(); Console.WriteLine("edit customer ran"); sqCommand.ExecuteNonQuery(); sqConnection.Close(); }