Пример #1
0
        private void btnAlterar_Click(object sender, EventArgs e)
        {
            if (txtCodigo.Text.Length == 0)
            {
                MessageBox.Show("Um item deve ser selecionado para alteração.");
            }
            else
            {
                try

                {
                    ItensTema itensTema = new ItensTema();

                    itensTema.IdItensTema = int.Parse(txtCodigo.Text);

                    itensTema.IdTema    = (int)cmbTema.SelectedValue;
                    itensTema.Nomeitem  = txtDescricao.Text;
                    itensTema.ValorItem = float.Parse(txtValor.Text);

                    ItensTemaBll obj = new ItensTemaBll();

                    obj.Alterar(itensTema);

                    MessageBox.Show("O item foi alterado com sucesso!");

                    AtualizaGrid();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro: " + ex.ToString());
                }
            }
        }
Пример #2
0
        private void btnIncluir_Click(object sender, EventArgs e)
        {
            try
            {
                ItensTema itensTema = new ItensTema();

                itensTema.IdTema    = (int)cmbTema.SelectedValue;
                itensTema.Nomeitem  = txtDescricao.Text;
                itensTema.ValorItem = float.Parse(txtValor.Text);



                ItensTemaBll obj = new ItensTemaBll();
                obj.Incluir(itensTema);

                MessageBox.Show("O item foi incluído com sucesso!");

                txtCodigo.Text = Convert.ToString(itensTema.IdItensTema);
                AtualizaGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro: " + ex.Message);
            }
        }
        //Incluir
        public void Incluir(ItensTema item)
        {
            //O nome do usuario é obrigatório
            //trim retira espaços a esquerda e a direita
            if (item.IdTema.ToString().Length == 0)
            {
                throw new Exception("O Codigo do Tema é obrigatório");
            }
            if (item.IdItensTema.ToString().Length == 0)
            {
                throw new Exception("O codigo do tema obrigatório");
            }
            if (item.Nomeitem.Trim().Length == 0)
            {
                throw new Exception("O nome do item é obrigatório");
            }
            if (item.ValorItem.ToString().Length == 0)
            {
                throw new Exception("O valor do item é obrigatório");
            }

            //Se tudo está Ok, chama a rotina de inserção.
            ItensTemaDal obj = new ItensTemaDal();

            obj.Incluir(item);
        }
        public void Incluir(ItensTema itensTema)

        {
            //conexao

            SqlConnection cn = new SqlConnection();

            try

            {
                cn.ConnectionString = Dados.StringDeConexao;

                //command
                SqlCommand cmd = new SqlCommand();
                cmd.Connection  = cn;
                cmd.CommandText = "insert into TbItensTema(TbTema_idTema, nomeDoItem,valorDoItem) values (@idTema, @nomeDoItem,@valorDoItem); select @@IDENTITY;";

                cmd.Parameters.AddWithValue("@idTema", itensTema.IdTema);
                cmd.Parameters.AddWithValue("@nomeDoItem", itensTema.Nomeitem);
                cmd.Parameters.AddWithValue("@valorDoItem", itensTema.ValorItem);

                cn.Open();

                itensTema.IdItensTema = Convert.ToInt32(cmd.ExecuteScalar());
            }

            catch (SqlException ex)
            {
                throw new Exception("Servidor SQL Erro:" + ex.Number);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }
        public void Alterar(ItensTema itensTema)
        {
            // conexao
            SqlConnection cn = new SqlConnection();

            try
            {
                cn.ConnectionString = Dados.StringDeConexao;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection  = cn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "update TbItensTema set TbTema_idTema = @idTema, nomeDoItem = @nomeDoItem, valorDoItem = @valorDoItem where idItensTema = @idItensTema;";

                cmd.Parameters.AddWithValue("@idTema", itensTema.IdTema);
                cmd.Parameters.AddWithValue("@nomeDoItem", itensTema.Nomeitem);
                cmd.Parameters.AddWithValue("@valorDoItem", itensTema.ValorItem);
                cmd.Parameters.AddWithValue("@idItensTema", itensTema.IdItensTema);


                cn.Open();

                cmd.ExecuteNonQuery();
            }

            catch (SqlException ex)
            {
                throw new Exception("Servidor SQL Erro:" + ex.ToString());
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }
        //Alterar
        public void Alterar(ItensTema item)
        {
            ItensTemaDal obj = new ItensTemaDal();

            obj.Alterar(item);
        }