protected void btnCadastro_Click(object sender, EventArgs e) { try { Dal.Entity.Medicacao m = new Dal.Entity.Medicacao(); //classe de entidade... m.Nome = txtMedicamento.Text; m.Descricao = txtDescricao.Text; MedicacaoDal mm = new MedicacaoDal(); //persistencia... mm.Insert(m); //gravando... lblMensagem.Text = "Medicamento " + m.Nome + ", cadastrado com sucesso."; //limpar os campos... txtMedicamento.Text = ""; txtDescricao.Text = ""; } catch (Exception ex) { //imprimir mensagem de erro... lblMensagem.Text = ex.Message; } }
public void Insert(Medicacao m) { try { OpenConnection(); //abrir conexão... Cmd = new SqlCommand("insert into Medicamentos(Nome, Descricao) values(@v1, @v2)", Con); Cmd.Parameters.AddWithValue("@v1", m.Nome); Cmd.Parameters.AddWithValue("@v2", m.Descricao); Cmd.ExecuteNonQuery(); //executar... } catch (Exception e) { //lançar exceção... throw new Exception("Erro ao inserir Medicamentos: " + e.Message); } finally { CloseConnection(); //fechar conexão.. } }