1
Answer

Asp.net C# windows application printing issue....

Photo of vaquas khan

vaquas khan

13y
4k
1
Dear all,

I need to print a record to dot matrix printer through windows application .....

my code is given below ...

  private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }
        public void Printing()
        {
            string txt="this is my line";

            try
            {
                streamToPrint = new StreamWriter (txt);
                try
                {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler
                       (this.pd_PrintPage);
                    pd.Print();
                }
                finally
                {
                    streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

but i want to 50 line per page ,suppose when first time i click the button then its print one line and when again i click the button then print 2 line .....etc  when i reached the 50th record then its allow to printer page break other wise it must be print in same page.

please help me

thanks

Answers (1)

0
Photo of Soft Corner
NA 3.1k 484.2k 13y
Hi,
 Try this Code :
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=DBNAME;Integrated Security=true;");

SqlDataReader dr;
SqlCommand cmd;

try
{
  con.Open();
  string qry ="select * from table where username ='"+textbox1.Text+"'";
  cmd = SqlCommand(qry,con);
  dr = cmd.ExecuteReader();
 while(dr.read())
  {
    //do something
   
MessageBox.Show("UserName:"+dr.GetString(0));
  }
  dr.Close();
  con.Close();
}
catch(Exception ex)
{
  MessageBox.Show(ex.message);
}
finally
{
con.Close();
}
Accepted
0
Photo of Abhimanyu K Vatsa
NA 50.9k 12.4m 13y
i think you are wishing to search out the record when user type in textbox and clicks button, isn't it? if so then read a article which let u find record from database

read this