Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The number of columns in PdfPTable constructor must be greater than zero


What I have tried:

<pre>

private static DataTable dt = new DataTable();
/////////////////////////////////////////////////////////


private static void slanjeizvestaja()
        {
            String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=bss_prijava_radnika;Integrated Security=True";
            using (SqlConnection openCon = new SqlConnection(cs))

            {

                string pronadji = "SELECT p.brojprijavnice2 AS Redni_broj, p.radnik AS Radnik, p.vrijemeprijave AS Vrijeme_Prijave, p.vrijemeodjave AS Vrijeme_odjave"
                                 + " FROM dbo.prijava_radnika p WHERE p.status = 'odjavljen'";



                openCon.Open();
                using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(pronadji, cs))

                {
                    DataTable dt = new DataTable();
                    querySaveStaff.Fill(dt);
                }
                openCon.Close();
            }
        }

        private static void sendEmail()
        {


            PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
            pdfTable.DefaultCell.Padding = 3;
            pdfTable.WidthPercentage = 100;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;
            BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 12);
Posted
Updated 13-Mar-18 6:52am

Your slanjeizvestaja declares a new variable called dt which masks the private static version that sendEmail is using for its data:
C#
using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(pronadji, cs))

{
    DataTable dt = new DataTable();
    querySaveStaff.Fill(dt);
Becasue of that, your data is thrown away at the end of the method.
Try:
C#
using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(pronadji, cs))

{
    dt = new DataTable();
    querySaveStaff.Fill(dt);
And see if that fixes it.
 
Share this answer
 
Comments
Goran Bibic 13-Mar-18 12:53pm    
Thank you
OriginalGriff 13-Mar-18 13:09pm    
You're welcome!
The "dt" in here

{
    DataTable dt = new DataTable();
    querySaveStaff.Fill(dt);
}


Only exists in that code block (between the { } ), so you create dt, populate it, then throw it away. So this code here;

PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);


is using the "dt" defined here

private static DataTable dt = new DataTable();


That dt remains untouched so has zero columns and no data. Change the code in slanjeizvestaja to use the dt defined at the top rather than creating a second "dt"

{
    dt = new DataTable();
    querySaveStaff.Fill(dt);
}
 
Share this answer
 
Comments
Goran Bibic 13-Mar-18 12:53pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900