Loops and if Else not working as per logic in c#
  • Hello
    i am creating a program that prints specific data from a database to printer. i want to print 1 row per page so i used a loop to do so. But loop is not working Ok and it is going out of its index. Please see below image for details what i want to print and what it is printing

    image

    In above 1st Expected Result is the actual and desired result i want to achieve. and at right side is the result by below code. Please help me in fixing this code.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WareHouseInventory.printing
    {
    public partial class all_product_barcodes : Form
    {

    // DataTable dt = new DataTable();
    PaperSize paperSize = new PaperSize("papersize", 210, 100);//set the paper size
    int totalnumberA=0;//this is for total number of items of the list or array
    int itemperpage;//this is for no of item per page

    public all_product_barcodes()
    {
    InitializeComponent();
    }
    SqlCommand cm;
    DataTable dt = new DataTable();

    public void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
    {

    int xValue = 50;
    int yValue = 20;
    int yValue2 = 35;
    int yValue3 = 65;
    int i;

    // {
    if (totalnumberA < dt.Rows.Count)
    {
    // foreach (DataRow dr in dt.Rows)
    // for (i = 0; i < dt.Rows.Count - 1; i++)
    for (i = 0; i < dt.Rows.Count ; i++)
    {
    string cell = dt.Rows[totalnumberA][0].ToString();
    // string itemx = dr["item_id"].ToString().PadLeft(6,'0');
    string itemx = cell.PadLeft(6, '0');
    string item2 = "*" + itemx + "*";
    string item3 = "*UMS" + itemx + "*";
    e.Graphics.DrawString("Do Not Remove", new Font("Arial", 8, FontStyle.Regular), Brushes.Black, xValue+30, yValue);
    e.Graphics.DrawString(item2, new Font("Free 3 of 9", 30, FontStyle.Regular), Brushes.Black, xValue, yValue2);
    e.Graphics.DrawString(item3, new Font("Arial", 14, FontStyle.Regular), Brushes.Black, xValue+5, yValue3);
    // yValue = yValue + 70;
    // yValue2 = yValue2 + 70;
    e.HasMorePages = true;
    totalnumberA++; return;
    }
    }
    }


    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    DBConnection DB = new DBConnection();
    DB.cnTransact.Open();
    // string sql = "select * from tbl_items where item_id='15'";
    string sql = "select * from tbl_items where serialNo='122'";
    //string sql = "select * from tbl_items";
    cm = new SqlCommand(sql, DB.cnTransact);
    SqlDataAdapter people = new SqlDataAdapter(cm);
    people.Fill(dt);

    //foreach (DataRow dr in dt.Rows)
    }


    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {

    }


    PrintDialog dialog = new PrintDialog();
    PrintDocument printDocument = new PrintDocument();
    dialog.Document = printDocument;
    printDocument.DefaultPageSettings.PaperSize = paperSize;
    dialog.ShowDialog();

    printDocument.PrintPage += PrintDocumentOnPrintPage;
    printDocument.DocumentName = "Barcodes";
    printDocument.Print();

    }

    }
    }



    Please tel me how can i solve this. i have shared complete code at above link. Thanks
  • 4 Comments sorted by
  • kindly implement this logic

    if(i==dt.Rows.Count-1)
    {
    e.HasMorePages = false;
    }
    else
    {
    e.HasMorePages = true;
    }

    where you have e.HasMorePages = true;
  • Hello @waqas
    Thanks for your reply. I tried all these things .. 
    If i use this still for loop is working for more than conditioned iterations and then at last it shows an exception saying There is no row at position 5.
    Please check and reply. Please use my above code and then reply to question. Thanks 
  • Try this with return when HasMorePages = true;

    if(i==dt.Rows.Count-1)
    {
    e.HasMorePages = false;

    }
    else
    {
    e.HasMorePages = true;
    totalnumberA++; return;
    }
  • Remove loop and change your code to

    public void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
    {
        int xValue = 50;
        int yValue = 20;
        int yValue2 = 35;
        int yValue3 = 65;
        if (totalnumberA < dt.Rows.Count)
        {
            string cell = dt.Rows[totalnumberA][0].ToString();
            string itemx = cell.PadLeft(6, '0');
            string item2 = "*" + itemx + "*";
            string item3 = "*UMS" + itemx + "*";
            e.Graphics.DrawString("Do Not Remove", new Font("Arial", 8, FontStyle.Regular), Brushes.Black, xValue+30, yValue);
            e.Graphics.DrawString(item2, new Font("Free 3 of 9", 30, FontStyle.Regular), Brushes.Black, xValue, yValue2);
            e.Graphics.DrawString(item3, new Font("Arial", 14, FontStyle.Regular), Brushes.Black, xValue+5, yValue3);

            if (totalnumberA == dt.Rows.Count - 1)
            {
                e.HasMorePages = false;
            }
            else
            {
                e.HasMorePages = true;
            }
            totalnumberA++; return;
        }
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Top Posters Weekly

Optimum Creative