sajid raja

sajid raja

  • NA
  • 66
  • 47.4k

c# Print Dialog not printing all pages

Oct 17 2014 4:47 PM
Hello
i want to print Values from database using print dialog in c#.
i want to print print 1 value at a page but it is not working. It only prints 2 pages and then Prints all values at first page. Please tell me what should i do with this. 
My code is
 
At Print Page Event
 
PaperSize paperSize = new PaperSize("papersize", 200, 100);  //page size 2X1 inches approx
int totalnumber2 = 0; 
 
public void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
{
SqlCommand cm;
DataTable dt = new DataTable();
try
{
DBConnection DB = new DBConnection();
DB.cnTransact.Open();
string sql = "select * from tbl_items";
cm = new SqlCommand(sql, DB.cnTransact);
SqlDataAdapter people = new SqlDataAdapter(cm);
people.Fill(dt);
int xValue = 25;
int yValue = 30;
int yValue2 = 65;
float pageheight = e.MarginBounds.Height;
int x = 0;
foreach (DataRow dr in dt.Rows)
{
string itemx = dr["item_id"].ToString().PadLeft(6,'0'); ;
string item2 = "*"+itemx+"*";
string item3 = "*UMS" + itemx + "*";
e.Graphics.DrawString(item2, new Font("Free 3 of 9", 30, FontStyle.Regular), Brushes.Black, xValue, yValue);
e.Graphics.DrawString(item3, new Font("Courier New", 14, FontStyle.Regular), Brushes.Black, xValue, yValue2);
if (totalnumber2 < dt.Rows.Count)
{
e.HasMorePages =true;
}
else {
e.HasMorePages = false;
}
totalnumber2++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
 
 
At Button Click Event
 
    private void btn1_Click(object sender, EventArgs e)
{
totalnumber2 = 0;
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 tell me where i am doing mistakes and help me in solving this.
Thanks