private void enterDataCompany(CompanyDTO company) { tbName.Text = company.BusinessName; cbDocumentType.SelectedItem = company.DocumentType; tbDocument.Text = company.Document.ToString(); tbAddress.Text = company.Address; tbActivity.Text = company.Activity; dtpPeriodFrom.Value = company.PeriodFrom; dtpPeriodTo.Value = company.PeriodTo; nudZipCode.Value = company.ZipCode; }
public FormModifyCompany(DataValidation dataValidation, CompanyDTO company) : base(dataValidation) { InitializeComponent(); bAccept.Text = "Modificar"; cbDocumentType.Enabled = false; dtpPeriodFrom.Enabled = false; tbDocument.Enabled = false; this.company = (company.Clone() as CompanyDTO); enterDataCompany(this.company); }
private void bAccept_Click(object sender, EventArgs e) { if (validate_data()) { int? id = this.company == null ? null : this.company.Id; this.company = new CompanyDTO(id, (DocumentTypeDTO)cbDocumentType.SelectedItem, long.Parse(tbDocument.Text), dtpPeriodFrom.Value, dtpPeriodTo.Value, (int)nudZipCode.Value, tbName.Text, tbActivity.Text, tbAddress.Text); try { this.dataValidation.saveCompany(company); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public void saveCompany(CompanyDTO company) { try { if (company.Id == null) { company.Id = this.data.addCompany(company); foreach (AccountRangeTemplateDTO template in this.data.getAccountRangeTemplates()) this.data.addAccountRange(template.toAccountRangeDTO((int)company.Id)); } else { this.data.modifyCompany(company); } } catch (Exception) { throw new ArgumentException("Ya existe una compañía con el mismo documento."); } }
public void modifyCompany(CompanyDTO company) { StringBuilder query = new StringBuilder(); query.AppendLine("UPDATE Companies"); query.AppendLine("SET DocumentTypeId = @documentTypeId, Document = @document, ActivityPeriodFrom = @periodFrom, ActivityPeriodTo = @periodTo, ZipCode = @zipCode, BusinessName = @bussinessName, Activity = @activity, Address = @address"); query.AppendLine("WHERE Id = @id;"); SqlCommand command = new SqlCommand(query.ToString(), this.conection); command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@id", company.Id); command.Parameters.AddWithValue("@documentTypeId", company.DocumentType.Id); command.Parameters.AddWithValue("@document", company.Document); command.Parameters.AddWithValue("@periodFrom", company.PeriodFrom); command.Parameters.AddWithValue("@periodTo", company.PeriodTo); command.Parameters.AddWithValue("@zipCode", company.ZipCode); command.Parameters.AddWithValue("@bussinessName", company.BusinessName); command.Parameters.AddWithValue("@activity", company.Activity); command.Parameters.AddWithValue("@address", company.Address); try { this.conection.Open(); command.ExecuteNonQuery(); } finally { if (this.conection.State == ConnectionState.Open) this.conection.Close(); } }
public int addCompany(CompanyDTO company) { StringBuilder query = new StringBuilder(); query.AppendLine("INSERT INTO Companies"); query.AppendLine("(DocumentTypeId, Document, ActivityPeriodFrom, ActivityPeriodTo, ZipCode, BusinessName, Activity, Address)"); query.AppendLine("VALUES (@documentTypeId, @document, @periodFrom, @periodTo, @zipCode, @businessName, @activity, @address);"); query.AppendLine("SELECT SCOPE_IDENTITY();"); SqlCommand command = new SqlCommand(query.ToString(), this.conection); command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@documentTypeId", company.DocumentType.Id); command.Parameters.AddWithValue("@document", company.Document); command.Parameters.AddWithValue("@periodFrom", company.PeriodFrom); command.Parameters.AddWithValue("@periodTo", company.PeriodTo); command.Parameters.AddWithValue("@zipCode", company.ZipCode); command.Parameters.AddWithValue("@businessName", company.BusinessName); command.Parameters.AddWithValue("@activity", company.Activity); command.Parameters.AddWithValue("@address", company.Address); int id; try { this.conection.Open(); id = (int)(decimal)command.ExecuteScalar(); } finally { if (this.conection.State == ConnectionState.Open) this.conection.Close(); } return id; }
public List<CompanyDTO> selectCompanyByName(string name) { StringBuilder query = new StringBuilder(); query.AppendLine("SELECT C.Id, C.DocumentTypeId, DT.Type DocumentType, Document, ActivityPeriodFrom, ActivityPeriodTo, ZipCode, BusinessName, Activity, Address FROM Companies C"); query.AppendLine("INNER JOIN DocumentType DT ON DT.Id = C.DocumentTypeId"); query.AppendLine("WHERE @name = '' OR CHARINDEX(@name, BusinessName) > 0;"); SqlCommand command = new SqlCommand(query.ToString(), this.conection); command.CommandType = System.Data.CommandType.Text; command.Parameters.AddWithValue("@name", name); List<CompanyDTO> companies = new List<CompanyDTO>(); try { this.conection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { CompanyDTO company = new CompanyDTO( (int)reader["Id"], new DocumentTypeDTO((int)reader["DocumentTypeId"], reader["DocumentType"].ToString()), (long)reader["Document"], (DateTime)reader["ActivityPeriodFrom"], (DateTime)reader["ActivityPeriodTo"], (int)reader["ZipCode"], reader["BusinessName"].ToString(), reader["Activity"].ToString(), reader["Address"].ToString()); companies.Add(company); } } } } finally { if (this.conection.State == ConnectionState.Open) this.conection.Close(); } return companies; }