public CntrlUser()
 {
     InitializeComponent();
     CntrlUserPnlEdit.Hide();
     edit = false;
     cntrlUserTxtName.Text       = "Imię";
     CntrlUserTxtSurname.Text    = "Nazwisko";
     CntrlUserTxtMail.Text       = "Mail";
     CntrlUserTxtPhone.Text      = "XXX-XXX-XXX";
     CntrlUserCmbPermission.Text = "Użytkownik";
 }
 private void CntrlUserBtnAddUser_Click(object sender, EventArgs e)
 {
     ChangeState();
     if (edit == true)
     {
         CntrlUserPnlEdit.Show();
     }
     else
     {
         CntrlUserPnlEdit.Hide();
     }
 }
        private void CntrlUserAddUser_Click(object sender, EventArgs e)
        {
            if (id == 0) //customer does not exist in database
            {
                using (SqlConnection connection = new SqlConnection(@"Data Source=GSZWEDPC\SQLEXPRESS;Initial Catalog=WeddingManagmentDB;Integrated Security=True"))
                {
                    connection.Open();
                    var query = @"INSERT INTO PeapleTable (Name, Surname, Mail, Phone, Permission) VALUES (@param1,@param2,@param3,@param4,@param5)";
                    using (SqlCommand cmd = new SqlCommand(query, connection))
                    {
                        cmd.Parameters.AddWithValue("@param1", cntrlUserTxtName.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param2", CntrlUserTxtSurname.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param3", CntrlUserTxtMail.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param4", CntrlUserTxtPhone.Text);
                        cmd.Parameters.AddWithValue("@param5", CntrlUserCmbPermission.Text.ToLower());

                        cmd.ExecuteNonQuery();
                    }

                    //get id of created person
                    query = @"SELECT id FROM PeapleTable WHERE Name = '" + cntrlUserTxtName.Text + "'AND Surname = '" + CntrlUserTxtSurname.Text + "'AND Mail = '" + CntrlUserTxtMail.Text + "'";
                    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
                    DataTable      table   = new DataTable();
                    adapter.Fill(table);
                    if (table.Rows.Count == 1)
                    {
                        DataRow row = table.Rows[0];
                        id = (int)row[0];
                        FillLabels(id);
                    }
                    connection.Close();
                }
                //clear all the textboxes
                cntrlUserTxtName.Text       = string.Empty;
                CntrlUserTxtSurname.Text    = string.Empty;
                CntrlUserTxtMail.Text       = string.Empty;
                CntrlUserTxtPhone.Text      = string.Empty;
                CntrlUserCmbPermission.Text = string.Empty;
                ChangeState();
                CntrlUserPnlEdit.Hide();
            }
            else // customer edit
            {
                using (SqlConnection connection = new SqlConnection(@"Data Source=GSZWEDPC\SQLEXPRESS;Initial Catalog=WeddingManagmentDB;Integrated Security=True"))
                {
                    connection.Open();
                    var query = @"UPDATE PeapleTable SET Name = @param1, Surname = @param2, Mail = @param3, Phone = @param4, Permission = @param5 WHERE id = @param6";
                    using (SqlCommand cmd = new SqlCommand(query, connection))
                    {
                        cmd.Parameters.AddWithValue("@param1", cntrlUserTxtName.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param2", CntrlUserTxtSurname.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param3", CntrlUserTxtMail.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param4", CntrlUserTxtPhone.Text);
                        cmd.Parameters.AddWithValue("@param5", CntrlUserCmbPermission.Text.ToLower());
                        cmd.Parameters.AddWithValue("@param6", id);

                        cmd.ExecuteNonQuery();
                    }
                    connection.Close();
                    FillLabels(id);
                    cntrlUserTxtName.Text       = string.Empty;
                    CntrlUserTxtSurname.Text    = string.Empty;
                    CntrlUserTxtMail.Text       = string.Empty;
                    CntrlUserTxtPhone.Text      = string.Empty;
                    CntrlUserCmbPermission.Text = string.Empty;
                    ChangeState();
                    CntrlUserPnlEdit.Hide();
                }
            }
        }