Example #1
0
        private void save_Click(object sender, RoutedEventArgs e)
        {
            string Fname       = txtFName.Text;
            string Lname       = txtLName.Text;
            string email       = txtEmail.Text;
            string phoneNumber = txtPhone.Text;

            if (string.IsNullOrEmpty(Fname) || string.IsNullOrEmpty(Lname) ||
                string.IsNullOrEmpty(email) || string.IsNullOrEmpty(phoneNumber))
            {
                MessageBox.Show("Please fill all fields.!");
                return;
            }
            Regex phone = new Regex(@"[a-zA-Z]");

            if (phone.IsMatch(phoneNumber))
            {
                MessageBox.Show("Phone number cannot include letters, try again");
                return;
            }
            var res = dbContext.Add(Fname, Lname, email, phoneNumber);

            if (res)
            {
                MessageBox.Show("Record Added Successfully.");
            }
            txtFName.Text = string.Empty;
            txtLName.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtPhone.Text = string.Empty;
        }
        //import contact from cvs file
        private void import_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            List <int>     list           = new List <int>();

            openFileDialog.DefaultExt = ".csv";
            if (openFileDialog.ShowDialog() == true)
            {
                string path     = openFileDialog.FileName;
                var    dataRows = File.ReadAllLines(path);

                for (int i = 0; i < dataRows.Length; i++)
                {
                    var columns = dataRows[i].Split(',');

                    try
                    {
                        string fname  = columns[0];
                        string Lname  = columns[1];
                        string Phone  = columns[2];
                        string email  = columns[3];
                        bool   result = dbContext.Add(fname, Lname, email, Phone);
                        if (!result)
                        {
                            list.Add(i + 1);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }
                if (list.Count > 0)
                {
                    string text = "System could not import following rows ";
                    foreach (var item in list)
                    {
                        text += item + " ";
                    }
                    MessageBox.Show(text);
                }
                else
                {
                    MessageBox.Show("Import Successfully..!");
                }
                UpdateGrid();
            }
        }