我在尝试运行.exe c#pdf表单填充时遇到此问题。该软件应从php页面获取2个ID变量,然后使用本地数据库(xampp localhost)中的数据填充pdf表单。当页面试图执行程序时,它只是因此错误而崩溃。在这里我可能会弄错的任何想法? (新手程序员)。
编辑:我认为我设法解决了有效和开放的连接问题,但现在我已经开启了连接:(编辑2:不,同样的错误。
编辑3:最后我设法使连接正常工作。但是我得到System.IndexOutOfRangeException:索引超出了数组的范围。每当我尝试运行程序时出错。我认为数组是空的由于某种原因。我猜测PHP脚本不会发送我需要的SELECT查询的id,并将id的manualy设置为1(int idagent = 1)但仍然没有运气。任何想法为什么?另一个问题是它有一种方法来检查我的PHP脚本是否发送我需要这个脚本的ID?谢谢:))
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;
using MySql.Data.MySqlClient;
namespace PdfGenerator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FillForm();
}
/// <summary>
/// List all of the form fields into a textbox. The
/// form fields identified can be used to map each of the
/// fields in a PDF.
/// </summary>
private void ListFieldNames()
{
string pdfTemplate = @"C:\xampp\htdocs\site\pdfgen\PdfGenerator\form.pdf";
// title the form
this.Text += " - " + pdfTemplate;
// create a new PDF reader based on the PDF template document
PdfReader pdfReader = new PdfReader(pdfTemplate);
// create and populate a string builder with each of the
// field names available in the subject PDF
StringBuilder sb = new StringBuilder();
foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
// Write the string builder's content to the form's textbox
textBox1.Text = sb.ToString();
textBox1.SelectionStart = 0;
}
public static int Param1 { get; set; }
public static int Param2 { get; set; }
private void FillForm()
{
int idfirma = Param1;
int idagent = Param2;
using (var connection = new MySqlConnection("server = localhost; User Id = root; password = ; database = documente;"))
{
string agenti = "SELECT * FROM `agenti` WHERE `ID` = @id";
connection.Open();
using (var cmd = new MySqlCommand(agenti, connection))
{
cmd.Parameters.AddWithValue("@ID", idagent);
using (MySqlDataReader result = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
var myArray = new string[,] { };
var counter = 0;
while (result.Read())
{
myArray[counter, 0] = (string)result["Nume Agent"];
myArray[counter, 1] = (string)result["Telefon"];
myArray[counter, 2] = (string)result["Sigiliu"];
myArray[counter, 3] = (string)result["Legitimatie"];
myArray[counter, 4] = (string)result["Prefix"];
counter++;
}
result.Close();
string datef = "SELECT * FROM `dateclienti` WHERE `ID` = @id";
using (var cmds = new MySqlCommand(datef, connection))
{
cmds.Parameters.AddWithValue("@ID", idfirma);
connection.Open();
using (MySqlDataReader result2 = cmds.ExecuteReader(CommandBehavior.CloseConnection))
{
var counter2 = 0;
var myArray2 = new string[,] { };
while (result2.Read())
{
myArray2[counter2, 0] = (string)result2["Nume Societate"];
myArray2[counter2, 1] = (string)result2["Adresa Sediu"];
myArray2[counter2, 2] = (string)result2["Adresa Punct de lucru 1"];
myArray2[counter2, 3] = (string)result2["Adresa Punct de lucru 2"];
myArray2[counter2, 4] = (string)result2["Registul Comertului"];
myArray2[counter2, 5] = (string)result2["Cod Fiscal"];
myArray2[counter2, 6] = (string)result2["E-Mail"];
myArray2[counter2, 7] = (string)result2["Cont Banca"];
myArray2[counter2, 8] = (string)result2["Tip casa de marcat"];
myArray2[counter2, 9] = (string)result2["Model casa de marcat"];
myArray2[counter2, 10] = (string)result2["Data de achizitie"];
myArray2[counter2, 11] = (string)result2["Configuratie"];
myArray2[counter2, 12] = (string)result2["Serie casa"];
myArray2[counter2, 13] = (string)result2["Serie fiscala"];
myArray2[counter2, 14] = (string)result2["Nume Pers. De contact"];
myArray2[counter2, 15] = (string)result2["Functie"];
myArray2[counter2, 16] = (string)result2["Serie CI"];
myArray2[counter2, 17] = (string)result2["CNP"];
myArray2[counter2, 18] = (string)result2["Eliberat de"];
myArray2[counter2, 19] = (string)result2["La data de"];
myArray2[counter2, 20] = (string)result2["Telefon"];
myArray2[counter2, 21] = (string)result2["Data"];
counter2++;
}
result2.Close();
string pdfTemplate = @"C:\xampp\htdocs\site\pdfgen\PdfGenerator\form.pdf";
string newFile = @"C:\xampp\htdocs\site\pdfgen\PdfGenerator\completed_form.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
// The first worksheet and W-4 form
pdfFormFields.SetField("topmostSubform[0].Page1[0].regcomert[0]", myArray[4,4]);
pdfFormFields.SetField("topmostSubform[0].Page1[0].adresafirmabenef[0]", "@adresasediu");
pdfFormFields.SetField("topmostSubform[0].Page1[0].dataa[0]", "@data");
pdfFormFields.SetField("topmostSubform[0].Page1[0].numeagent[0]", "@numeagent");
pdfFormFields.SetField("topmostSubform[0].Page1[0].codfiscal[0]", "@ro");
pdfFormFields.SetField("topmostSubform[0].Page2[0].numefirmabenef[0]", "@numefirmabenef");
pdfFormFields.SetField("topmostSubform[0].Page1[0].fuctie[0]", "@functie");
pdfFormFields.SetField("topmostSubform[0].Page1[0].reprezentatfirma[0]", "@repfirma");
pdfFormFields.SetField("topmostSubform[0].Page1[0].tipcasademarc[0]", "@tipcasa");
pdfFormFields.SetField("topmostSubform[0].Page1[0].dataa[1]", "@data");
pdfFormFields.SetField("topmostSubform[0].Page1[0].numefirmabenef[0]", "@numefirmabenef");
pdfFormFields.SetField("topmostSubform[0].Page1[0].nrtelrepfirma[0]", "@nrtelfirma");
pdfFormFields.SetField("topmostSubform[0].Page1[0].modelcasamarc[0]", "@modelcasa");
pdfFormFields.SetField("topmostSubform[0].Page1[0].nrpv[0]", "SRN1001");
pdfFormFields.SetField("topmostSubform[0].Page1[0].telefonagent[0]]", "@telefonagent");
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = true;
// close the pdf
pdfStamper.Close();
}
}
}
}
}
}
}
}
答案 0 :(得分:0)
您需要在某个时刻实际打开连接。该命令不会为您执行此操作:
using (var connection = new MySqlConnection("server = localhost; User Id = root; password = ; database = documente;"))
{
connection.Open(); // open connection before using it
string agenti = "SELECT * FROM `agenti` WHERE `ID` = @id";
using (var cmd = new MySqlCommand(agenti, connection))
{
cmd.Parameters.AddWithValue("@ID", idagent);
using (MySqlDataReader result = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
var myArray = new string[] { };
while (result.Read())
{
myArray[0] = (string)result["Nume Agent"];
myArray[1] = (string)result["Telefon"];
myArray[2] = (string)result["Sigiliu"];
myArray[3] = (string)result["Legitimatie"];
myArray[4] = (string)result["Prefix"];
}
result.Close();
}
}
}
然后,您将在第二个查询中遇到问题。因为您在第一个查询中设置了CommandBehavior.CloseConnection
,所以在执行第二个查询之前需要再次打开连接。