-1

Why the rectangle didn't draw? Is there any mistake in my code? I try to create a rectangle through SQLDataReader while clause..

private void PrintsDoc (object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    StringFormat sf = new StringFormat ();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Near;

    Font fontStyle = new Font ("Times New Roman", 30);
    Font fontStyle2 = new Font ("Times New Roman", 10);
    g.DrawString ("Trade Confirmation", fontStyle, Brushes.Black, e.MarginBounds, sf);
    g.DrawString ("\n\n\nTrade Date: " + dt + "\nPrint Date: " + DateTime.Now.ToString (), fontStyle2, Brushes.Black, e.MarginBounds, sf);

    SqlConnection conn = new SqlConnection("Server=WORKERCOMP\\SQLEXPRESS;" + "Database=database;" + "User ID=uid;" + "Password=pwd;" + "Trusted_Connection=yes;");
    conn.Open ();

    int i = 0, j = 0;
    SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'dbo.TRADE'", conn);
    SqlDataReader rdr = cmd.ExecuteReader ();
    while (rdr.Read ()) {
        Rectangle rec = new Rectangle (new Point (40 + i, 250), new Size (200, 25));
        g.DrawRectangle (Pens.Black, rec);
        g.DrawString (rdr.GetString(0 + j), fontStyle2, Brushes.Black, rec, sf);
        i = i + 215;
        j = j + 1;
    }

    e.HasMorePages = false;
    conn.Close ();
}
Hobas Matius
  • 61
  • 2
  • 13
  • Go debugging. Place breakpoints, step through your code, inspect your variable. If the `while()` isn't executed, your query yields no results. – CodeCaster Sep 22 '15 at 10:49
  • Hey, I've tested that SQLCommand in SQL Server Management Studio, and it has results.. So I think the while clause should be running.. But why the rectangle didn't draw – Hobas Matius Sep 22 '15 at 10:56
  • Are you sure you tried it in SSMS? TABLE_NAME doesn't include SCHEMA_NAME. – Cetin Basoz Sep 22 '15 at 10:59
  • No, don't guess. Debug. – CodeCaster Sep 22 '15 at 10:59
  • Thanks for everyone who voted down my question. I've just learned programming for about couple months, and I think that votedown shows you didn't appreciate someone that willing to learn. – Hobas Matius Sep 22 '15 at 11:05
  • I am not one of downvoters, and you are right this downvoting on stackoverflow sucks. Especially when they do it just because they don't understand the question (while others might). – Cetin Basoz Sep 22 '15 at 11:41

1 Answers1

0
private void PrintsDoc (object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    StringFormat sf = new StringFormat ();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Near;

    Font fontStyle = new Font ("Times New Roman", 30);
    Font fontStyle2 = new Font ("Times New Roman", 10);
    g.DrawString ("Trade Confirmation", fontStyle, Brushes.Black, e.MarginBounds, sf);
    g.DrawString ("\n\n\nTrade Date: " + dt + "\nPrint Date: " + DateTime.Now.ToString (), fontStyle2, Brushes.Black, e.MarginBounds, sf);

    using (SqlConnection conn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=database;Trusted_Connection=yes;"))
    using (SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TRADE'", conn))
    {
    conn.Open ();
    int i = 0, j = 0;
    SqlDataReader rdr = cmd.ExecuteReader ();
    while (rdr.Read ()) {
        Rectangle rec = new Rectangle (new Point (40 + i, 250), new Size (200, 25));
        g.DrawRectangle (Pens.Black, rec);
        //g.DrawString (rdr.GetString(0 + j), fontStyle2, Brushes.Black, rec, sf);
        g.DrawString ((string)rdr["COLUMN_NAME"], fontStyle2, Brushes.Black, rec, sf);
        i = i + 215;
        j = j + 1; // what is the purpose of j?
    }

    e.HasMorePages = false;
    conn.Close ();
    }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39