我在做什么? pdfptable构造函数中的列数必须大于零 [英] What I mising? The number of columns in pdfptable constructor must be greater than zero
本文介绍了我在做什么? pdfptable构造函数中的列数必须大于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
推荐答案
您的slanjeizvestaja
声明一个名为dt
的新变量,它掩盖了sendEmail $ c $的私有静态版本c>用于其数据:
Yourslanjeizvestaja
declares a new variable calleddt
which masks the private static version thatsendEmail
is using for its data:
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:
using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(pronadji, cs))
{
dt = new DataTable();
querySaveStaff.Fill(dt);
看看是否修复了它。
这里的dt
The "dt" in here
{
DataTable dt = new DataTable();
querySaveStaff.Fill(dt);
}
只存在于该代码块中({}之间),所以你创建了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);
使用此处定义的dt
is using the "dt" defined here
private static DataTable dt = new DataTable();
该dt保持不变,因此零列没有数据。更改slanjeizvestaja中的代码以使用顶部定义的dt而不是创建第二个dt
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);
}
这篇关于我在做什么? pdfptable构造函数中的列数必须大于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文