private void ArtigoFavoritar(int codArtigo, int pFavorito)
        {
            try
            {
                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    string strSQL = @"
                        UPDATE TB_ARTIGO
                        SET ARTI_FAVORITO = " + pFavorito + " WHERE ARTI_CODIGO = " + codArtigo;

                    SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);

                    sqlCmd.ExecuteNonQuery();
                }
                else
                {
                    MessageBox.Show(pstrMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void LstCategoriaPreencher()
        {
            SqlConnection cnn          = new SqlConnection();
            string        pstrMsg      = "";
            bool          pbooRetorno  = false;
            string        titCategoria = txtCategoria.Text;

            cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

            if (pbooRetorno)
            {
                string strSQL = @"
                SELECT * FROM 
                (
                SELECT 0 CATE_CODIGO, '(TODOS)' CATE_NOME
                UNION ALL
                SELECT 
	                C.CATE_CODIGO, C.CATE_NOME
                FROM TB_CATEGORIA C
                ) CatOrdenado 
                WHERE 1 = 1 ";


                if (!string.IsNullOrEmpty(titCategoria))
                {
                    strSQL += "AND CatOrdenado.CATE_NOME LIKE '%" + titCategoria + "%' ";
                }

                strSQL += "ORDER BY CatOrdenado.CATE_NOME ";

                SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);

                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable      dt = new DataTable("TB_CATEGORIA");

                da.Fill(dt);

                lstCategorias.ValueMember   = "CATE_CODIGO";
                lstCategorias.DisplayMember = "CATE_NOME";
                lstCategorias.DataSource    = dt;
            }
            else
            {
                MessageBox.Show(pstrMsg);

                if (!pbooRetorno)
                {
                    Close();
                }
            }

            //GridArtigosPreencher();

            cnn.Close();
        }
        private void LstCategoriaPreencher()
        {
            try
            {
                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    string strSQL = @"
                    SELECT * FROM 
                    (
                    SELECT 0 CATE_CODIGO, '' CATE_NOME
                    UNION ALL
                    SELECT CATE_CODIGO, CATE_NOME 
                    FROM TB_CATEGORIA
                    ) CarOrdenado
                    ORDEr BY CarOrdenado.CATE_NOME ";

                    SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);

                    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                    DataTable      dt = new DataTable("TB_CATEGORIA");

                    da.Fill(dt);

                    cmbCategoria.ValueMember   = "CATE_CODIGO";
                    cmbCategoria.DisplayMember = "CATE_NOME";
                    cmbCategoria.DataSource    = dt;
                }
                else
                {
                    MessageBox.Show(pstrMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (!pbooRetorno)
                    {
                        Close();
                    }
                }

                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void ArtigoCadastro_Load(object sender, EventArgs e)
        {
            LstCategoriaPreencher();

            if (!ArtigoCodigo.Equals(0))
            {
                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    string strSQL = @"
                    SELECT A.ARTI_NOME, C.CATE_NOME, A.ARTI_TEXTO
                    FROM TB_ARTIGO A
                    INNER JOIN TB_CATEGORIA C ON C.CATE_CODIGO = A.ARTI_CATE_CODIGO
                    WHERE A.ARTI_CODIGO = " + ArtigoCodigo;

                    SqlCommand    sqlCmd      = new SqlCommand(strSQL, cnn);
                    SqlDataReader sqlDataRead = sqlCmd.ExecuteReader();

                    while (sqlDataRead.Read())
                    {
                        txtTitulo.Text = sqlDataRead.GetValue(0).ToString();

                        int index = cmbCategoria.FindString(sqlDataRead.GetValue(1).ToString());

                        cmbCategoria.SelectedIndex = index;
                        txtTexto.Text = sqlDataRead.GetValue(2).ToString();
                    }
                }
                else
                {
                    MessageBox.Show(pstrMsg);

                    if (!pbooRetorno)
                    {
                        Close();
                    }
                }

                cnn.Close();
            }

            RedimencionaObjetos();
        }
        private void btnSalvarImagem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ImagemValidaCampos())
                {
                    return;
                }

                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    imlImagens.Images.Add(txtImagemDescricao.Text, picImagemVisualizador.Image);
                    lsvImagens.Items.Clear();

                    for (int i = 0; i < imlImagens.Images.Count; i++)
                    {
                        lsvImagens.Items.Add(new ListViewItem {
                            ImageIndex = i, Text = txtImagemDescricao.Text
                        });
                    }

                    ArtigoLimpaCampos();
                }
                else
                {
                    MessageBox.Show(pstrMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (!pbooRetorno)
                    {
                        Close();
                    }
                }

                cnn.Close();
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void CategoriaSalvar()
        {
            try
            {
                // Valida se o(s) campo(s) obrigatórios foram preenchidos
                if (!ValidaCampos())
                {
                    return;
                }

                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    string strSQL = @"
                    INSERT INTO TB_CATEGORIA 
                    VALUES ('" + txtNome.Text + "','" + txtDescricao.Text + "')";

                    SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);

                    sqlCmd.ExecuteNonQuery();

                    MessageBox.Show("A categoria '" + txtNome.Text + "' foi incluída com sucesso!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LimpaCampos();
                }
                else
                {
                    MessageBox.Show(pstrMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (!pbooRetorno)
                    {
                        Close();
                    }
                }

                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ArtigoVisualizador_Load(object sender, EventArgs e)
        {
            if (!ArtigoCodigo.Equals(0))
            {
                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    string strSQL = @"
                    SELECT A.ARTI_NOME, C.CATE_NOME, A.ARTI_TEXTO
                    FROM TB_ARTIGO A
                    INNER JOIN TB_CATEGORIA C ON C.CATE_CODIGO = A.ARTI_CATE_CODIGO
                    WHERE A.ARTI_CODIGO = " + ArtigoCodigo;

                    SqlCommand    sqlCmd      = new SqlCommand(strSQL, cnn);
                    SqlDataReader sqlDataRead = sqlCmd.ExecuteReader();

                    while (sqlDataRead.Read())
                    {
                        lblTítulo.Text    = sqlDataRead.GetValue(0).ToString();
                        lblCategoria.Text = sqlDataRead.GetValue(1).ToString();
                        lblDescricao.Text = sqlDataRead.GetValue(2).ToString();
                    }
                }
                else
                {
                    MessageBox.Show(pstrMsg);

                    if (!pbooRetorno)
                    {
                        Close();
                    }
                }

                cnn.Close();
            }

            Redimencionador();
        }
        private async Task ArtigoExcluirAsync(int codArtigo)
        {
            pnlInfo.Visible = true;
            ExcluirButtonEnable();

            try
            {
                await AcaoDelay();

                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    string strSQL = @"
                    DELETE FROM TB_ARTIGO
                    WHERE ARTI_CODIGO = " + codArtigo;

                    SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);
                    sqlCmd.ExecuteNonQuery();

                    GridArtigosPreencher();
                }
            }
            catch (TaskCanceledException ex)
            {
                if (ex.CancellationToken.IsCancellationRequested)
                {
                    GridArtigosPreencher();
                }
            }
            finally
            {
                pnlInfo.Visible = false;
                ExcluirButtonEnable(true);
            }
        }
        private void ArtigoSalvar()
        {
            try
            {
                // Valida se o(s) campo(s) obrigatórios foram preenchidos
                if (!ArtigoValidaCampos())
                {
                    return;
                }

                SqlConnection cnn         = new SqlConnection();
                string        pstrMsg     = "";
                bool          pbooRetorno = false;

                cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

                if (pbooRetorno)
                {
                    if (ArtigoCodigo.Equals(0))
                    {
                        string strSQL = @"
                        INSERT INTO TB_ARTIGO 
                        VALUES ('" + txtTitulo.Text + "','" + txtTexto.Text + "'," + cmbCategoria.SelectedValue + ")";

                        SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);

                        sqlCmd.ExecuteNonQuery();

                        MessageBox.Show("O artigo de título '" + txtTitulo.Text + "' foi incluído com sucesso!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        string strSQL = @"
                        UPDATE TB_ARTIGO 
                        SET ARTI_NOME = '" + txtTitulo.Text + "', ARTI_TEXTO = '" + txtTexto.Text.Replace("'", "") + "', ARTI_CATE_CODIGO = " + cmbCategoria.SelectedValue + " WHERE ARTI_CODIGO = " + ArtigoCodigo;

                        SqlCommand sqlCmd = new SqlCommand(strSQL, cnn);

                        sqlCmd.ExecuteNonQuery();

                        MessageBox.Show("O artigo de título '" + txtTitulo.Text + "' foi alterado com sucesso!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        Close();
                    }

                    ArtigoLimpaCampos();
                }
                else
                {
                    MessageBox.Show(pstrMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (!pbooRetorno)
                    {
                        Close();
                    }
                }

                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#10
0
        public void GridArtigosPreencher()
        {
            grdArtigos.Columns.Clear();
            grdArtigos.DataSource = null;

            SqlConnection cnn         = new SqlConnection();
            string        pstrMsg     = "";
            bool          pbooRetorno = false;

            int    idCategoria = int.Parse(lstCategorias.SelectedValue.ToString());
            string titArtigo   = txtArtigo.Text;

            cnn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

            if (pbooRetorno)
            {
                string strSQL = @"
                    SELECT  
                    A.ARTI_FAVORITO, A.ARTI_CODIGO, A.ARTI_NOME, A.ARTI_CATE_CODIGO, C.CATE_NOME
                    FROM TB_ARTIGO A 
                    INNER JOIN TB_CATEGORIA C ON C.CATE_CODIGO = A.ARTI_CATE_CODIGO 
                    WHERE 1 = 1 ";

                if (idCategoria != 0)
                {
                    strSQL += "AND C.CATE_CODIGO = " + idCategoria + " ";
                }

                if (!string.IsNullOrEmpty(titArtigo))
                {
                    strSQL += "AND A.ARTI_NOME LIKE '%" + titArtigo + "%' ";
                }

                strSQL += "ORDER BY A.ARTI_NOME ";

                SqlCommand     sqlCmd      = new SqlCommand(strSQL, cnn);
                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);

                grdArtigos.DataSource = dtRecord;

                GridArtigosFormatar();

                cnn.Close();

                if (dtRecord.Rows.Count == 0)
                {
                    MessageBox.Show("Nenhum registro foi encontrado para esse filtro!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show(pstrMsg);

                if (!pbooRetorno)
                {
                    Close();
                }
            }
        }