try{
/* QUERY TO COMPLETE PAGE 13*/
sqlCodeOther = @"SELECT
a.objectID,
LTRIM(RTRIM(a.att12)) 'LastName',
LTRIM(RTRIM(a.att32)) 'FirstName',
LTRIM(RTRIM(a.att42)) 'ID',
LTRIM(RTRIM(b.att65)) 'Car Name',
LTRIM(RTRIM(b.att45)) 'Pol Number'
FROM [db].dbo.stance1029 a LEFT OUTER JOIN [db].dbo.stance1048 b ON a.objectid = b.fk14";
}
catch (Exception ce)
{
}
using (SqlCommand command = new SqlCommand(sqlCodeOther, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
isDone = true;
if (reader.Read())
{
//fills in PDF form fields (ROW 1)
pdfFormFields.SetField("PROFLIABILITYCARRIERpg13", reader.GetValue(4).ToString()); //117
pdfFormFields.SetField("PROFLIABILITYCARRIER2pg13", reader.GetValue(4).ToString()); //191
pdfFormFields.SetField("POLICYNUMBERpg13", reader.GetValue(5).ToString()); //323
//fills in PDF form fields (ROW 2)
//HOW TO?
//pdfFormFields.SetField("PROFLIABILITYCARRIERpg14", reader.GetValue(?).ToString()); //145
//pdfFormFields.SetField("PROFLIABILITYCARRIER2pg14", reader.GetValue(?).ToString()); //356
//pdfFormFields.SetField("POLICYNUMBERpg14", reader.GetValue(?).ToString()); //512
}
}
}
reader = null;
}
The above C# code is querying and populating a PDF form fields with the data. The issue I am running into is the query might return multiple rows but right now I am only able to read the first row and populate the PDF form. How do I then get the second row and populate another part of the form and so forth until I have read all the rows and populated the respective section in the PDF form?
Instead of
if (reader.Read())
Use
while(reader.Read())
In order to handle the different pdf fields you could try something like:
int rowNum = 13;
while(reader.Read())
{
pdfFormFields.SetField("PROFLIABILITYCARRIERpg" + rowNum.ToString(), reader.GetValue(4).ToString());
pdfFormFields.SetField("PROFLIABILITYCARRIER2pg" + rowNum.ToString(), reader.GetValue(4).ToString());
pdfFormFields.SetField("POLICYNUMBERpg" + rowNum.ToString(), reader.GetValue(5).ToString());
rowNum++;
}
The above would depend on if you want the same sql fields to the same pdf fields, but only change the index of the fields based on the index of the row...
For reading multiple rows you need to iterate. Replace your if
with while
like:
while (reader.Read())
Also there shouldn't be any need to enclose sqlCodeOther
initialization inside a try-catch
, if only you are trying to assign a hard coded string to your variable.