0

I have a button click event:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        conn.Open();
        string query = "INSERT INTO bank_check_kassa OUTPUT INSERTED.formatted_id default values";
        SqlCommand command = new SqlCommand(query, conn);
        string insertedID = (string)command.ExecuteScalar();
        label1.Padding = new Padding(-50, 0, 0, 0);
        label1.Text = "Novbedeki siraniz " + insertedID.ToString();

        // Printer ayarları
        using (PrintDocument pd = new PrintDocument())
        {
            // Yazdırma işlemi
            pd.PrintPage += (s, args) => ProvideContent1(s, args, insertedID);
            pd.Print();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error " + ex);
    }
    finally
    {
        conn.Close();
    }
}

private void ProvideContent1(object sender, PrintPageEventArgs e, string insertedID)
{
    // Создание шрифта для текста
    Font font = new Font("Arial", 12, FontStyle.Regular);

    // Создание кисти для текста
    SolidBrush brush = new SolidBrush(Color.Black);

    // Определение координаты для вывода текста
    float x = 100;
    float y = 100;

    // Вывод текста на документ
    e.Graphics.DrawString("Novbedeki siraniz: " + insertedID, font, brush, x, y);
}

This works, in that information on the label will appear. However, I want to print the information immediately, and for some reason it doesn't go to print. Instead, I see the print dialog. What I want is to mark the printer by default so that the test can print without waiting (skip the dialog).

How can I do this?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Filburt Apr 06 '23 at 13:38
  • 1
    fwiw, it's usually a mistake to try to re-use the same connection object. ADO.Net already does connection pooling. Re-using the connection object interferes with that and makes things slower, rather than helping. You're better off allocating new connection objects for each query in a `using` block. If it's more about hiding the connection string, you can do that by making a method to return the newly-created connection. I also suggest closing the connection before moving on to printing. – Joel Coehoorn Apr 06 '23 at 13:39
  • 1
    Are you asking how to print something to the default system printer? Or otherwise predetermine the printer to use so that there's no user interaction required after clicking the button? – phuzi Apr 06 '23 at 13:40
  • 1
    Does this answer your question: https://stackoverflow.com/questions/17590896/auto-print-without-dialog – phuzi Apr 06 '23 at 13:45

0 Answers0