public DataTable Search(CustomerDistrict customerDistrict)
        {
            DataTable dataTable = new DataTable();

            try
            {
                //Connection
                string        connectionString = @"Server=DESKTOP-LE66CE0; Database=CustomerInfo; Integrated Security=True";
                SqlConnection sqlConnection    = new SqlConnection(connectionString);

                //Command
                //INSERT INTO Items (Name, Price) Values ('Black', 120)
                //string commandString = @"SELECT * FROM Items WHERE Name='" + name + "'";
                string     commandString = @"SELECT * FROM Customers WHERE Name='" + customerDistrict.Name + "'";
                SqlCommand sqlCommand    = new SqlCommand(commandString, sqlConnection);

                //Open
                sqlConnection.Open();

                //Show
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                //DataTable dataTable = new DataTable();
                sqlDataAdapter.Fill(dataTable);


                //Close
                sqlConnection.Close();
            }
            catch (Exception exeption)
            {
                //MessageBox.Show(exeption.Message);
            }

            return(dataTable);
        }
        public bool Update(CustomerDistrict customerDistrict)
        {
            try
            {
                //Connection
                string        connectionString = @"Server=DESKTOP-LE66CE0; Database=CustomerInfo; Integrated Security=True";
                SqlConnection sqlConnection    = new SqlConnection(connectionString);

                //Command
                //UPDATE Items SET Name =  'Hot' , Price = 130 WHERE ID = 1
                string     commandString = @"UPDATE Customers SET Name =  '" + customerDistrict.Name + "' , Address = '" + customerDistrict.Address + "', Contact = '" + customerDistrict.Contact + "' , District = '" + customerDistrict.District + "' WHERE Code = '" + customerDistrict.Code + "'";
                SqlCommand sqlCommand    = new SqlCommand(commandString, sqlConnection);

                //Open
                sqlConnection.Open();

                //Insert
                int isExecuted = sqlCommand.ExecuteNonQuery();
                if (isExecuted > 0)
                {
                    return(true);
                }
                //Close
                sqlConnection.Close();
            }
            catch (Exception exeption)
            {
                //MessageBox.Show(exeption.Message);
            }
            return(false);
        }
Esempio n. 3
0
 private void searchButton_Click(object sender, EventArgs e)
 {
     if (nameTextBox.Text == "")
     {
         MessageBox.Show("Enter search name");
     }
     else
     {
         CustomerDistrict customerDistrict = new CustomerDistrict();
         customerDistrict.Name           = nameTextBox.Text;
         customerDataGridView.DataSource = _customerManager.Search(customerDistrict);
     }
 }
        public bool AddCustomer(CustomerDistrict customerDistrict)
        {
            bool isAdded = false;

            string        connectionString = @"Server=DESKTOP-LE66CE0; Database= CustomerInfo; Integrated Security= True";
            SqlConnection sqlConnection    = new SqlConnection(connectionString);

            sqlConnection.Open();

            string     commandString = @"INSERT INTO Customers (Code,Name,Address,Contact,District) VALUES ('" + customerDistrict.Code + "','" + customerDistrict.Name + "','" + customerDistrict.Address + "','" + customerDistrict.Contact + "','" + customerDistrict.District + "')";
            SqlCommand sqlCommand    = new SqlCommand(commandString, sqlConnection);

            int isExecuted = sqlCommand.ExecuteNonQuery();

            if (isExecuted > 0)
            {
                isAdded = true;
            }
            sqlConnection.Close();
            return(isAdded);
        }
        public bool IsContactExists(CustomerDistrict customerDistrict)
        {
            string        connectionString = @"Server=DESKTOP-LE66CE0; Database= CustomerInfo; Integrated Security= True";
            SqlConnection sqlConnection    = new SqlConnection(connectionString);

            bool isExists = false;

            string     commandString = @"SELECT Contact FROM Customers WHERE Contact= '" + customerDistrict.Contact + "'";
            SqlCommand sqlCommand    = new SqlCommand(commandString, sqlConnection);

            sqlConnection.Open();
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            DataTable      dataTable      = new DataTable();

            sqlDataAdapter.Fill(dataTable);
            if (dataTable.Rows.Count > 0)
            {
                isExists = true;
            }
            sqlConnection.Close();
            return(isExists);
        }
 public DataTable Search(CustomerDistrict customerDistrict)
 {
     return(_customerRepository.Search(customerDistrict));
 }
 public bool UpdateCustomer(CustomerDistrict customerDistrict)
 {
     return(_customerRepository.Update(customerDistrict));
 }
 public bool IsContactExists(CustomerDistrict customerDistrict)
 {
     return(_customerRepository.IsContactExists(customerDistrict));
 }
 public bool AddCustomer(CustomerDistrict customerDistrict)
 {
     return(_customerRepository.AddCustomer(customerDistrict));
 }
Esempio n. 10
0
        private void saveButton_Click_1(object sender, EventArgs e)
        {
            CustomerDistrict customerDistrict = new CustomerDistrict();

            if (saveButton.Text == "Save")
            {
                int i;
                if (String.IsNullOrEmpty(codeTextBox.Text) || !int.TryParse(codeTextBox.Text, out i) || String.IsNullOrEmpty(nameTextBox.Text) || String.IsNullOrEmpty(addressTextBox.Text) || String.IsNullOrEmpty(contactTextBox.Text) || String.IsNullOrEmpty(customerComboBox.Text))
                {
                    MessageBox.Show("Fill the empty box");
                }
                else
                {
                    customerDistrict.Code = Convert.ToInt32(codeTextBox.Text);
                    if (_customerManager.IsCodeExists(customerDistrict))
                    {
                        MessageBox.Show("Code " + codeTextBox.Text + " is Already exist");
                    }
                    else
                    {
                        customerDistrict.Contact = contactTextBox.Text;
                        if (_customerManager.IsContactExists(customerDistrict))
                        {
                            MessageBox.Show("Contact no " + contactTextBox.Text + " is Already exist");
                        }
                        else
                        {
                            customerDistrict.Name     = nameTextBox.Text;
                            customerDistrict.Address  = addressTextBox.Text;
                            customerDistrict.District = customerComboBox.Text;

                            bool isAdded = _customerManager.AddCustomer(customerDistrict);

                            if (isAdded)
                            {
                                MessageBox.Show("Data Saved Successfully");
                                customerDataGridView.DataSource = _customerManager.DisplayData();
                                codeTextBox.Text      = "";
                                nameTextBox.Text      = "";
                                addressTextBox.Text   = "";
                                contactTextBox.Text   = "";
                                customerComboBox.Text = "select";
                            }
                            else
                            {
                                MessageBox.Show("not Saved");
                            }
                        }
                    }
                }
            }
            else if (saveButton.Text == "Update")
            {
                int i;
                if (String.IsNullOrEmpty(codeTextBox.Text) || !int.TryParse(codeTextBox.Text, out i))
                {
                    MessageBox.Show("code must be an integer");
                    return;
                }
                if (nameTextBox.Text == "")
                {
                    MessageBox.Show("Name can't be empty");
                    return;
                }
                if (addressTextBox.Text == "")
                {
                    MessageBox.Show("Address can't be empty");
                    return;
                }
                if (contactTextBox.Text == "")
                {
                    MessageBox.Show("Contact can't be empty");
                    return;
                }
                if (customerComboBox.Text == "")
                {
                    MessageBox.Show("ple select your district");
                    return;
                }
                customerDistrict.Code = Convert.ToInt32(codeTextBox.Text);

                customerDistrict.Name     = nameTextBox.Text;
                customerDistrict.Address  = addressTextBox.Text;
                customerDistrict.Contact  = contactTextBox.Text;
                customerDistrict.District = customerComboBox.Text;


                if (_customerManager.UpdateCustomer(customerDistrict))
                {
                    MessageBox.Show("Updated");
                    customerDataGridView.DataSource = _customerManager.DisplayData();
                    saveButton.Text       = "Save";
                    codeTextBox.Text      = "";
                    nameTextBox.Text      = "";
                    addressTextBox.Text   = "";
                    contactTextBox.Text   = "";
                    customerComboBox.Text = "Select";
                }
                else
                {
                    MessageBox.Show("not updated");
                }
            }
        }