public Company(int companyId) { this.id = -1; this.mailContact = string.Empty; this.web = string.Empty; this.subscriptionEnd = DateTime.Now; this.subscriptionStart = DateTime.Now; this.name = string.Empty; this.language = "es"; using (SqlCommand cmd = new SqlCommand("Company_GetById")) { cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["cns"].ConnectionString); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@CompanyId", SqlDbType.Int); cmd.Parameters["@CompanyId"].Value = companyId; try { cmd.Connection.Open(); SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.HasRows) { rdr.Read(); this.id = rdr.GetInt32(0); this.name = rdr.GetString(1); this.mailContact = string.Empty; this.web = string.Empty; this.subscriptionStart = rdr.GetDateTime(2); this.subscriptionEnd = rdr.GetDateTime(3); this.language = rdr.GetString(4); this.nif = rdr.GetString(5); } this.departments = Company.GetDepartments(this.id); this.addresses = CompanyAddress.GetAddressByCompanyId(this); foreach (CompanyAddress address in this.addresses) { if (address.DefaultAddress) { this.defaultAddress = address; break; } } this.GetEmployees(); } catch { this.id = -1; this.mailContact = string.Empty; this.web = string.Empty; this.subscriptionEnd = DateTime.Now; this.subscriptionStart = DateTime.Now; this.name = string.Empty; } finally { if (cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); } } } }
/// <summary>Initializes a new instance of the Company class. /// Company data is searched on database based in company identifier /// </summary> /// <param name="companyId">Company identifier</param> public Company(int companyId) { this.Id = -1; this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionEnd = DateTime.Now; this.SubscriptionStart = DateTime.Now; this.Name = string.Empty; this.Headquarters = string.Empty; this.Language = "es"; this.DiskQuote = 0; string source = string.Format(CultureInfo.InvariantCulture, "cto::Company({0})", companyId); using (var cmd = new SqlCommand("Company_GetById")) { using (var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cns"].ConnectionString)) { cmd.Connection = cnn; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@CompanyId", SqlDbType.Int); cmd.Parameters["@CompanyId"].Value = companyId; try { cmd.Connection.Open(); using (var rdr = cmd.ExecuteReader()) { if (rdr.HasRows) { rdr.Read(); this.Id = rdr.GetInt32(0); this.Name = rdr[1].ToString(); this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionStart = rdr.GetDateTime(2); this.SubscriptionEnd = rdr.GetDateTime(3); this.Language = Convert.ToString(rdr[4], CultureInfo.InvariantCulture); this.FiscalNumber = rdr[5].ToString(); this.Code = rdr[6].ToString(); this.DiskQuote = rdr.GetInt64(8); this.Agreement = rdr.GetBoolean(9); this.Headquarters = rdr.GetString(10); } this.departments = Company.ObtainDepartments(this.Id); this.addresses = CompanyAddress.GetAddressByCompanyId(this).ToList(); foreach (var address in this.addresses) { if (address.DefaultAddress) { this.defaultAddress = address; break; } } this.ObtainEmployees(); } } catch (SqlException ex) { ExceptionManager.Trace(ex, string.Format(CultureInfo.InstalledUICulture, "cto::Company({0})", companyId)); this.Id = -1; this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionEnd = DateTime.Now; this.SubscriptionStart = DateTime.Now; this.Name = string.Empty; this.Headquarters = string.Empty; } catch (FormatException ex) { ExceptionManager.Trace(ex, string.Format(CultureInfo.InstalledUICulture, "cto::Company({0})", companyId)); this.Id = -1; this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionEnd = DateTime.Now; this.SubscriptionStart = DateTime.Now; this.Name = string.Empty; this.Headquarters = string.Empty; } catch (NullReferenceException ex) { ExceptionManager.Trace(ex, string.Format(CultureInfo.InstalledUICulture, "cto::Company({0})", companyId)); this.Id = -1; this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionEnd = DateTime.Now; this.SubscriptionStart = DateTime.Now; this.Name = string.Empty; this.Headquarters = string.Empty; } catch (ArgumentNullException ex) { ExceptionManager.Trace(ex, string.Format(CultureInfo.InvariantCulture, "cto::Company({0})", companyId)); this.Id = -1; this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionEnd = DateTime.Now; this.SubscriptionStart = DateTime.Now; this.Name = string.Empty; } catch (ArgumentException ex) { ExceptionManager.Trace(ex, string.Format(CultureInfo.InvariantCulture, "cto::Company({0})", companyId)); this.Id = -1; this.MailContact = string.Empty; this.Web = string.Empty; this.SubscriptionEnd = DateTime.Now; this.SubscriptionStart = DateTime.Now; this.Name = string.Empty; this.Headquarters = string.Empty; } finally { if (cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); } } } } this.countries = new List <Country>(); using (var cmdCountries = new SqlCommand("Company_GetCountries")) { using (var cnnCountry = new SqlConnection(ConfigurationManager.ConnectionStrings["cns"].ToString())) { cmdCountries.Connection = cnnCountry; cmdCountries.CommandType = CommandType.StoredProcedure; try { cmdCountries.Parameters.Add(DataParameter.Input("@CompanyId", companyId)); cmdCountries.Connection.Open(); using (var rdrCountries = cmdCountries.ExecuteReader()) { while (rdrCountries.Read()) { this.countries.Add(new Country() { Id = rdrCountries.GetInt32(0), Description = rdrCountries.GetString(1) }); } } } catch (SqlException ex) { ExceptionManager.Trace(ex, source); } catch (FormatException ex) { ExceptionManager.Trace(ex, source); } catch (NullReferenceException ex) { ExceptionManager.Trace(ex, source); } catch (ArgumentNullException ex) { ExceptionManager.Trace(ex, source); } catch (ArgumentException ex) { ExceptionManager.Trace(ex, source); } finally { if (cmdCountries.Connection.State != ConnectionState.Closed) { cmdCountries.Connection.Close(); } } } } }