none
Geração de Etiquetas com base em um select do SQL RRS feed

  • Discussão Geral

  • Mais uma vez preciso da ajuda de vcs..

    No meu código abaixo faço impressão de etiquetas referente a uma ordem de produção que gerou 50 itens, então preciso gerar 50 etiquetas, só que na query que gera as etiquetas tem um campo que gera um código de barras e é um numero sequencial, preciso fazer um loop nesse código para gerar as 50 etiquetas referente ao código de barras existente na query.

    Do jeito que esta meu codigo só imprimi 1 etiqueta, não gera todas que esta no select.

    Sou novato em c# se puderem me mostrar o comando no meu código eu agradeço.

    segue as telas e o código completo.

     public partial class frmEtiquetas : Form
        {
            int largura;
            int altura;

            int Res;

            List<string> ListaImpEtiqueta = new List<string>();
            string sNomeImpressora = System.Configuration.ConfigurationSettings.AppSettings["ImpressoraPadrao"];

            public frmEtiquetas()
            {
                InitializeComponent();
            }

            private void frmEtiquetas_Load(object sender, EventArgs e)
            {

            }

            private void btnPesquisar_Click(object sender, EventArgs e)
            {
                vizualização.Document = EtiquetaProdPS;
                vizualização.InvalidatePreview();        
            }


            public string retornaCodBarras(string txtNGuiaFrac)
            {
                string ret = string.Empty;
                string connectionString = this.DataTable1TableAdapter.Connection.ConnectionString;

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    con.Open();
                    string strQuery = @" SELECT
                                         SC.C2_NUM,
                                         SB.B1_DESC,
                                         SC.C2_XNPAIS,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTFAB AS DATE),103) AS DATAF,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTVALI AS DATE),103) AS DATAV,
                                         SC.C2_XLOTEF,
                                         SC.C2_XLOTE,
                                         SB.B1_XDCB,
                                         SB.B1_XCAS,
                                         CAST(SB.B1_PESO AS NUMERIC(15, 3)) AS PESO,
                                         SB.B1_XSEGUM,
                                         SC.C2_XNOMFA,
                                         CB.CB0_CODETI,
                                         SC.C2_QUANT
                                  FROM SC2020 AS SC
                                  INNER JOIN SB1020 AS SB WITH (NOLOCK) ON SB.B1_COD = SC.C2_PRODUTO
                                  INNER JOIN CB0020 AS CB WITH (NOLOCK) ON CB.CB0_LOTE = SC.C2_XLOTE
                                  WHERE C2_NUM = '" + txtNGuiaFrac + "'";
                    using (SqlCommand command = new SqlCommand(strQuery, con))

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            reader.GetString(12);
                            ret = reader[12].ToString();

                            ListaImpEtiqueta.Add("Produto: " + (reader[1].ToString()) + "                                             " +"N° Guia: " + reader[0].ToString());
                            ListaImpEtiqueta.Add("Fabricação: " +(reader[3].ToString()) + "              " + "Validade: " + reader[4].ToString() + "              " + "Qtda. : " + reader[9].ToString() + reader[10].ToString());
                            ListaImpEtiqueta.Add("Lote Fabr.: " + (reader[5].ToString()) + "                                         " + "Lote Inter.: " + reader[6].ToString());
                            ListaImpEtiqueta.Add("Fabricante : " + (reader[11].ToString()) + "       " + "País Orig.: " + reader[2].ToString());
                            ListaImpEtiqueta.Add("DCB: " + (reader[7].ToString()) + "                                                        " + reader[12].ToString());
                            ListaImpEtiqueta.Add("Cod.: " + " * " + reader[12].ToString() + " * " );
                            ListaImpEtiqueta.Add("CAS: " + (reader[8].ToString().Insert(48, System.Environment.NewLine)));
                            break;

                        }
                    }
                    con.Close();
                }
                return ret;
            }

            public string retornaQunat(string txtNGuiaFrac)
            {
                string ret = string.Empty;
                string connectionString = this.DataTable1TableAdapter.Connection.ConnectionString;

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    con.Open();
                    string strQuery = @" SELECT
                                         SC.C2_NUM,
                                         SB.B1_DESC,
                                         SC.C2_XNPAIS,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTFAB AS DATE),103) AS DATAF,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTVALI AS DATE),103) AS DATAV,
                                         SC.C2_XLOTEF,
                                         SC.C2_XLOTE,
                                         SB.B1_XDCB,
                                         SB.B1_XCAS,
                                         CAST(SB.B1_PESO AS NUMERIC(15, 3)) AS PESO,
                                         SB.B1_XSEGUM,
                                         SC.C2_XNOMFA,
                                         CB.CB0_CODETI,
                                         SC.C2_QUANT
                                  FROM SC2020 AS SC
                                  INNER JOIN SB1020 AS SB WITH (NOLOCK) ON SB.B1_COD = SC.C2_PRODUTO
                                  INNER JOIN CB0020 AS CB WITH (NOLOCK) ON CB.CB0_LOTE = SC.C2_XLOTE
                                  WHERE C2_NUM = '" + txtNGuiaFrac + "'";
                    using (SqlCommand command = new SqlCommand(strQuery, con))

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            reader.GetDouble(13);
                            ret = reader[13].ToString();
                            break;
                        }
                    }
                    con.Close();
                }
                return ret;
            }

            private void txtNGuiaFrac_KeyDown(object sender, KeyEventArgs e)
            {
                    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
                    {
                        txtCodBarras.Text = retornaCodBarras(txtNGuiaFrac.Text);
                        txtQuant.Text = retornaQunat(txtNGuiaFrac.Text);
                        Res = Convert.ToInt32(txtCodBarras.Text) + Convert.ToInt32(txtQuant.Text);
                        txtCodBarrasFinal.Text = Res.ToString();
                        txtNGuiaFrac.Focus();
                    }
            }

            private void btnFechar_Click(object sender, EventArgs e)
            {
                Close();
            }

            private void btnImprimir_Click(object sender, EventArgs e)
            {
                EtiquetaProdPS.PrinterSettings.PrinterName = sNomeImpressora;
                EtiquetaProdPS.Print();     
            }

            private void EtiquetaProdPS_PrintPage(object sender, PrintPageEventArgs e)
            {
                    Font codBarras = new Font("Bar-Code 39", 14);
                    Font letra = new Font("Aerial", 9, FontStyle.Regular, GraphicsUnit.Pixel);
                    SolidBrush cor = new SolidBrush(Color.Black);
                    Rectangle retangulo = new Rectangle(12, 36, largura, 30);

                    int y = 53;
                    foreach (string frase in ListaImpEtiqueta)
                    {
                        if (frase.Contains("Cod"))
                        {
                            e.Graphics.DrawString(frase.Split(':')[1].ToString(), codBarras, cor, new Point(250, y));
                        }
                        else
                        {
                            e.Graphics.DrawString(frase, letra, cor, new Point(15, y));
                            y += 14;
                        }
                    }
                }

        }
    }


    JUNIOR GUERREIRO T.I


    quarta-feira, 1 de fevereiro de 2017 18:03

Todas as Respostas

  • Preciso mais uma vez da ajuda de vcs do forum.

    Estou usando System.Environment.NewLine para fazer um quebra de linha quando chega em 48 caractere ele passa pra linha de baixo, mas quando chega na linha debaixo, preciso que conte mais 48 caracteres, e passe novamente para linha de baixo, isso não esta acontecendo no meu código como mostro abaixo.

    código.

    ListaImpEtiqueta.Add("CAS: " + (reader[8].ToString().Insert(48, System.Environment.NewLine)));

    Resultado.


    JUNIOR GUERREIRO T.I

    quarta-feira, 1 de fevereiro de 2017 16:09
  • Bom dia Junio,

    Poste o seu código, por gentileza.

    Atenciosamente,


    Robson William Silva

    Esse conteúdo é fornecido sem garantias de qualquer tipo, seja expressa ou implícita

    MSDN Community Support

    Por favor, lembre-se de Marcar como Resposta as postagens que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.

    quinta-feira, 2 de fevereiro de 2017 11:26
    Moderador
  • Bom dia..

    Segue meu código.

    public partial class frmEtiquetas : Form
        {
            int largura;
            int altura;
    
            int Res;
    
            List<string> ListaImpEtiqueta = new List<string>();
            string sNomeImpressora = System.Configuration.ConfigurationSettings.AppSettings["ImpressoraPadrao"];
    
            public frmEtiquetas()
            {
                InitializeComponent();
            }
    
            private void frmEtiquetas_Load(object sender, EventArgs e)
            {
    
            }
    
            private void btnPesquisar_Click(object sender, EventArgs e)
            {
    
                int margemIn = CentimetrosParaCentesimasPolegada(50); // https://msdn.microsoft.com/pt-br/library/system.drawing.printing.pagesettings.margins(v=vs.110).aspx
                EtiquetaProdPS.DefaultPageSettings.Margins = new Margins(margemIn, margemIn, margemIn, margemIn);
    
    
                int larguraIn = CentimetrosParaCentesimasPolegada(2.53);
                int alturaIn = CentimetrosParaCentesimasPolegada(0.6);
                PaperSize paperSize = new PaperSize("EtiquetaPS", larguraIn, alturaIn);
                EtiquetaProdPS.DefaultPageSettings.PaperSize = paperSize;
                EtiquetaProdPS.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
                vizualização.Document = EtiquetaProdPS;
                vizualização.InvalidatePreview();        
            }
    
    
            public string retornaCodBarras(string txtNGuiaFrac)
            {
                string ret = string.Empty;
                string connectionString = this.DataTable1TableAdapter.Connection.ConnectionString;
    
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    con.Open();
                    string strQuery = @" SELECT 
                                         SC.C2_NUM,
                                         SB.B1_DESC,
                                         SC.C2_XNPAIS,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTFAB AS DATE),103) AS DATAF,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTVALI AS DATE),103) AS DATAV,
                                         SC.C2_XLOTEF,
                                         SC.C2_XLOTE,
                                         SB.B1_XDCB,
                                         SB.B1_XCAS,
                                         CAST(SB.B1_PESO AS NUMERIC(15, 3)) AS PESO,
                                         SB.B1_XSEGUM,
                                         SC.C2_XNOMFA,
                                         CB.CB0_CODETI,
                                         SC.C2_QUANT
                                  FROM SC2020 AS SC 
                                  INNER JOIN SB1020 AS SB WITH (NOLOCK) ON SB.B1_COD = SC.C2_PRODUTO
                                  INNER JOIN CB0020 AS CB WITH (NOLOCK) ON CB.CB0_LOTE = SC.C2_XLOTE
                                  WHERE C2_NUM = '" + txtNGuiaFrac + "'";
                    using (SqlCommand command = new SqlCommand(strQuery, con))
    
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
    
                        while (reader.Read())
                        {
                            reader.GetString(12);
                            ret = reader[12].ToString();
    
                            ListaImpEtiqueta.Add("Produto: " + (reader[1].ToString()) + "   " + "N° Guia: " + reader[0].ToString());
                            ListaImpEtiqueta.Add("Fabricação: " + (reader[3].ToString()) + "              " + "Validade: " + reader[4].ToString() + "              " + "Qtda. : " + reader[9].ToString() + reader[10].ToString());
                            ListaImpEtiqueta.Add("Lote Fabr.: " + (reader[5].ToString()) + "                                         " + "Lote Inter.: " + reader[6].ToString());
                            ListaImpEtiqueta.Add("Fabric.: " + (reader[11].ToString()) + " " + "País Orig.: " + reader[2].ToString());
                            ListaImpEtiqueta.Add("DCB: " + (reader[7].ToString()) + "                                         " + reader[12].ToString());
                            ListaImpEtiqueta.Add("Cod.: " + " * " + reader[12].ToString() + " * ");
                            ListaImpEtiqueta.Add("CAS: " + (reader[8].ToString().Insert(43, System.Environment.NewLine)));
                            ListaImpEtiqueta.Add("");
                            ListaImpEtiqueta.Add("");
                        }
                    }
                    con.Close();
                }
                return ret;
            }
    
            public string retornaQunat(string txtNGuiaFrac)
            {
                string ret = string.Empty;
                string connectionString = this.DataTable1TableAdapter.Connection.ConnectionString;
    
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    con.Open();
                    string strQuery = @" SELECT 
                                         SC.C2_NUM,
                                         SB.B1_DESC,
                                         SC.C2_XNPAIS,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTFAB AS DATE),103) AS DATAF,
                                         CONVERT(VARCHAR(10), CAST( SC.C2_XDTVALI AS DATE),103) AS DATAV,
                                         SC.C2_XLOTEF,
                                         SC.C2_XLOTE,
                                         SB.B1_XDCB,
                                         SB.B1_XCAS,
                                         CAST(SB.B1_PESO AS NUMERIC(15, 3)) AS PESO,
                                         SB.B1_XSEGUM,
                                         SC.C2_XNOMFA,
                                         CB.CB0_CODETI,
                                         SC.C2_QUANT
                                  FROM SC2020 AS SC 
                                  INNER JOIN SB1020 AS SB WITH (NOLOCK) ON SB.B1_COD = SC.C2_PRODUTO
                                  INNER JOIN CB0020 AS CB WITH (NOLOCK) ON CB.CB0_LOTE = SC.C2_XLOTE
                                  WHERE C2_NUM = '" + txtNGuiaFrac + "'";
                    using (SqlCommand command = new SqlCommand(strQuery, con))
    
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            reader.GetDouble(13);
                            ret = reader[13].ToString();
                            break;
                        }
                    }
                    con.Close();
                }
                return ret;
            }
    
            private void txtNGuiaFrac_KeyDown(object sender, KeyEventArgs e)
            {
                    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
                    {
                        txtCodBarras.Text = retornaCodBarras(txtNGuiaFrac.Text);
                        txtQuant.Text = retornaQunat(txtNGuiaFrac.Text);
                        Res = Convert.ToInt32(txtCodBarras.Text) + Convert.ToInt32(txtQuant.Text);
                        txtCodBarrasFinal.Text = Res.ToString();
                        txtNGuiaFrac.Focus();
                    }
            }
    
            private void btnFechar_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void btnImprimir_Click(object sender, EventArgs e)
            {
                int margemIn = CentimetrosParaCentesimasPolegada(50); // https://msdn.microsoft.com/pt-br/library/system.drawing.printing.pagesettings.margins(v=vs.110).aspx
                EtiquetaProdPS.DefaultPageSettings.Margins = new Margins(margemIn, margemIn, margemIn, margemIn);
    
                int larguraIn = CentimetrosParaCentesimasPolegada(2.53);
                int alturaIn = CentimetrosParaCentesimasPolegada(0.6);
                PaperSize paperSize = new PaperSize("EtiquetaPS", larguraIn, alturaIn);
                EtiquetaProdPS.DefaultPageSettings.PaperSize = paperSize;
                EtiquetaProdPS.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
                EtiquetaProdPS.PrinterSettings.PrinterName = sNomeImpressora;
                EtiquetaProdPS.Print();     
            }
    
            private static int CentimetrosParaCentesimasPolegada(double cm)
            {
                return (int)Math.Round(cm / 0.393701 * 100, MidpointRounding.AwayFromZero);
            }
    
            private void EtiquetaProdPS_PrintPage(object sender, PrintPageEventArgs e)
            {
                    Font codBarras = new Font("Bar-Code 39", 14);
                    Font letra = new Font("Aerial", 10, FontStyle.Regular, GraphicsUnit.Pixel);
                    SolidBrush cor = new SolidBrush(Color.Black);
                    Rectangle retangulo = new Rectangle(12, 36, largura, 30);
    
                    int y = 55;
                    foreach (string frase in ListaImpEtiqueta)
                    {
                        if (frase.Contains("Cod"))
                        {
                            e.Graphics.DrawString(frase.Split(':')[1].ToString(), codBarras, cor, new Point(250, y));
                        }
                        else
                        {
                            e.Graphics.DrawString(frase, letra, cor, new Point(20, y));
                            y += 14;
                        }
                    }
                }
    
        }
    }



    JUNIOR GUERREIRO T.I


    quinta-feira, 2 de fevereiro de 2017 11:28
  • Não tenho certeza que seja isso,

    mas no código acima onde é usado o Environment.NewLine está 43 e não 48:

    reader[8].ToString().Insert(43, System.Environment.NewLine)));

    Atenciosamente,


    Robson William Silva

    Esse conteúdo é fornecido sem garantias de qualquer tipo, seja expressa ou implícita

    MSDN Community Support

    Por favor, lembre-se de Marcar como Resposta as postagens que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.

    quinta-feira, 2 de fevereiro de 2017 11:50
    Moderador
  • A sim Robson e que ontem depois que eu postei tive que fazer  um ajuste nos caracteres pra quebrar a linha a gora esta com 43, mas quando desce pra segunda linha, não quebra novamente com 43, preciso que ele quebre a linha como quebrou na primeira linha. EX. 43 caracteres passar pra segunda linha, segunda linha deu 43 caracteres passa pra terceira linha e assim sucessivamente.

    JUNIOR GUERREIRO T.I

    quinta-feira, 2 de fevereiro de 2017 12:17