Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. [Table("cliente", Schema = "public")]
  2. public class Cliente
  3. {
  4. [Key]
  5. [Column("id")]
  6. public int Id { get; set; }
  7.  
  8. [Required(ErrorMessage = "Nome não pode ser nulo.")]
  9. [Column("nome")]
  10. public string Nome { get; set; }
  11.  
  12. [Required(AllowEmptyStrings = true)]
  13. [Column("endereco")]
  14. public string Endereco { get; set; }
  15.  
  16.  
  17. [Column("bairro", TypeName="text")]
  18. public string Bairro { get; set; }
  19.  
  20. [Required(ErrorMessage = "Cidade não pode ser nulo.")]
  21. [Column("cidade")]
  22. public int CidadeID { get; set; }
  23.  
  24. [ForeignKey("CidadeID")]
  25. public Cidade Cidade { get; set; }
  26.  
  27. [Column("cpfcnpj")]
  28. public string CPFCNPJ { get; set; }
  29.  
  30. [Column("telefone")]
  31. public string Telefone { get; set; }
  32.  
  33. [Column("ativo")]
  34. public bool Ativo { get; set; }
  35.  
  36. public virtual IQueryable<Cliente> Clientes { get; set; }
  37.  
  38. }
  39.  
  40. public static void InserirCliente(Cliente cli)
  41. {
  42. using (var db = new Repositorio.DBContexto())
  43. {
  44. try
  45. {
  46. db.Clientes.Add(cli);
  47. var usuarioSalvo = db.SaveChanges();
  48. }
  49. catch (Exception)
  50. {
  51. throw;
  52. }
  53. }
  54. }
  55.  
  56. private void btnSalvar_ItemClick(object sender, ItemClickEventArgs e)
  57. {
  58.  
  59. try
  60. {
  61. var cliente = new Cliente();
  62. cliente.Nome = Convert.ToString(txtNome.EditValue);
  63. cliente.Telefone = Convert.ToString(txtTelefone.EditValue);
  64. cliente.CPFCNPJ = Convert.ToString(txtCPF.EditValue);
  65.  
  66. cliente.Endereco = Convert.ToString(txtEndereco.EditValue);
  67. cliente.Bairro = Convert.ToString(txtBairro.EditValue);
  68. cliente.CidadeID = Convert.ToInt32(lkeCidade.EditValue);
  69. cliente.Ativo = true;
  70.  
  71. DAL.ClienteDAL.InserirCliente(cliente);
  72.  
  73. MessageBox.Show("Cliente Inserido com Sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
  74. this.DialogResult = DialogResult.OK;
  75. this.Close();
  76.  
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show(string.Format("{0}nn{1}", ex.Message, ex.InnerException), "Ooops", MessageBoxButtons.OK, MessageBoxIcon.Error);
  81. }
  82.  
  83. }
  84.  
  85. cliente.Bairro = (txtBairro.EditValue == null ? null : Convert.ToString(txtBairro.EditValue));
  86.  
  87. cliente.Nome = txtNome.EditValue;
  88.  
  89. Convert.ToInt32(lkeCidade.EditValue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement