hi my name is vishal i was wondering on how to bind results of another sql select query within same data reader execution in c# windows forms with sql server2008.
I at process of exporting c# sql server datas to pdf report in form of a table usingItextSharp and sql select query.
Given below is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Diagnostics;
using System.IO;
namespace DRRS_CSharp
{
public partial class MDIParent1 : Form
{
public MDIParent1()
{
InitializeComponent();
}
private void assignedDialyzerToolStripMenuItem_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4.Rotate());
var writer = PdfWriter.GetInstance(doc, new FileStream("AssignedDialyzer.pdf", FileMode.Create));
doc.SetMargins(70,70,70,70);
doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
doc.Open();
PdfPTable table = new PdfPTable(6);
table.TotalWidth =600f;
table.LockedWidth = true;
PdfPCell cell = new PdfPCell(new Phrase("Institute/Hospital:AIIMS,NEW DELHI", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK)));
cell.Colspan = 6;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
Paragraph para = new Paragraph("DCS Clinical Record-Assigned Dialyzer", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
para.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
png.ScaleToFit(105f, 105f);
png.Alignment = Element.ALIGN_RIGHT;
var NormalFont = FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.BOLD);
var regular = FontFactory.GetFont("Arial", 8, BaseColor.BLACK);
var phrase = new Phrase();
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
conn.Open();
SqlCommand cmd= new SqlCommand("Select d.dialyserID,r.errorCode,Convert(varchar,r.dialysis_date,106),pn.patient_first_name,pn.patient_last_name,d.manufacturer,d.dialyzer_size,r.start_date,r.end_date,d.packed_volume,r.bundle_vol,r.disinfectant,t.Technician_first_name,t.Technician_last_name from dialyser d,patient_name pn,reprocessor r,Techniciandetail t where pn.patient_id=d.patient_id and r.dialyzer_id=d.dialyserID and t.technician_id=r.technician_id and d.deleted_status=0 and pn.status=1 and r.reprocessor_id in (Select max(r.reprocessor_id) from reprocessor r where r.dialyzer_id=d.dialyserID) order by pn.patient_first_name,pn.patient_last_name", conn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
table.AddCell(new Phrase(new Chunk("Reprocessing Date", NormalFont)));
table.AddCell(new Phrase(new Chunk("Patient Name", NormalFont)));
table.AddCell(new Phrase(new Chunk("Dialyzer(Manufacturer,Size)", NormalFont)));
table.AddCell(new Phrase(new Chunk("No.of Reuse", NormalFont)));
table.AddCell(new Phrase(new Chunk("Verification", NormalFont)));
table.AddCell(new Phrase(new Chunk("DialyzerID", NormalFont)));
while (dr.Read())
{
table.AddCell(new Phrase(new Chunk(dr[2].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[3].ToString() + "-" + dr[4].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[5].ToString() + "-" + dr[6].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[12].ToString() + "-" + dr[13].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[0].ToString(),regular)));
}
dr.Close();
table.SpacingBefore = 22f;
doc.Add(para);
doc.Add(png);
doc.Add(table);
doc.Close();
System.Diagnostics.Process.Start("AssignedDialyzer.pdf");
if (MessageBox.Show("Do you want to save changes to AssignedDialyzer.pdf before closing?", "DRRS", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
var writer2 = PdfWriter.GetInstance(doc, new FileStream("AssignedDialyzer.pdf", FileMode.Create));
}
else if (MessageBox.Show("Do you want to save changes to AssignedDialyzer.pdf before closing?", "DRRS", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) == DialogResult.No)
{
this.Close();
}
}AssignedDialyzer.pdf is name of my pdf report file to which i am exporting c# sql server datas .
Now that in order to populate column No.of Reuse in my pdf report through c# windows forms with sql server2008 i found out a query that worked. Given below:
Select r.dialyzer_id,Count(r.dialyzer_id) from reprocessor r Group By dialyzer_id
Then i thought of executing it like below:
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SqlCommand hsd = new SqlCommand();
hsd.Connection = conn;
hsd.CommandType = CommandType.Text;
hsd=new SqlCommand("Select dialyzer_id,Count(dialyzer_id) from reprocessor Group By dialyzer_id",conn);
int result = ((int)cmd.ExecuteScalar());
conn.close();
}But when i try to integrate the result of result variable into reader execution as given below:
private void assignedDialyzerToolStripMenuItem_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4.Rotate());
var writer = PdfWriter.GetInstance(doc, new FileStream("AssignedDialyzer.pdf", FileMode.Create));
doc.SetMargins(70,70,70,70);
doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
doc.Open();
PdfPTable table = new PdfPTable(6);
table.TotalWidth =600f;
table.LockedWidth = true;
PdfPCell cell = new PdfPCell(new Phrase("Institute/Hospital:AIIMS,NEW DELHI", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK)));
cell.Colspan = 6;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
Paragraph para = new Paragraph("DCS Clinical Record-Assigned Dialyzer", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
para.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
png.ScaleToFit(105f, 105f);
png.Alignment = Element.ALIGN_RIGHT;
var NormalFont = FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.BOLD);
var regular = FontFactory.GetFont("Arial", 8, BaseColor.BLACK);
var phrase = new Phrase();
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
conn.Open();
SqlCommand cmd= new SqlCommand("Select d.dialyserID,r.errorCode,Convert(varchar,r.dialysis_date,106),pn.patient_first_name,pn.patient_last_name,d.manufacturer,d.dialyzer_size,r.start_date,r.end_date,d.packed_volume,r.bundle_vol,r.disinfectant,t.Technician_first_name,t.Technician_last_name from dialyser d,patient_name pn,reprocessor r,Techniciandetail t where pn.patient_id=d.patient_id and r.dialyzer_id=d.dialyserID and t.technician_id=r.technician_id and d.deleted_status=0 and pn.status=1 and r.reprocessor_id in (Select max(r.reprocessor_id) from reprocessor r where r.dialyzer_id=d.dialyserID) order by pn.patient_first_name,pn.patient_last_name", conn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
table.AddCell(new Phrase(new Chunk("Reprocessing Date", NormalFont)));
table.AddCell(new Phrase(new Chunk("Patient Name", NormalFont)));
table.AddCell(new Phrase(new Chunk("Dialyzer(Manufacturer,Size)", NormalFont)));
table.AddCell(new Phrase(new Chunk("No.of Reuse", NormalFont)));
table.AddCell(new Phrase(new Chunk("Verification", NormalFont)));
table.AddCell(new Phrase(new Chunk("DialyzerID", NormalFont)));
while (dr.Read())
{
table.AddCell(new Phrase(new Chunk(dr[2].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[3].ToString() + "-" + dr[4].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[5].ToString() + "-" + dr[6].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[12].ToString() + "-" + dr[13].ToString(),regular)));
table.AddCell(new Phrase(new Chunk(dr[0].ToString(),regular)));
}
dr.Close();
table.SpacingBefore = 22f;
doc.Add(para);
doc.Add(png);
doc.Add(table);
doc.Close();
System.Diagnostics.Process.Start("AssignedDialyzer.pdf");
if (MessageBox.Show("Do you want to save changes to AssignedDialyzer.pdf before closing?", "DRRS", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
var writer2 = PdfWriter.GetInstance(doc, new FileStream("AssignedDialyzer.pdf", FileMode.Create));
}
else if (MessageBox.Show("Do you want to save changes to AssignedDialyzer.pdf before closing?", "DRRS", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) == DialogResult.No)
{
this.Close();
}
}I get error telling:InvalidCastException was unhandled:specific cast is not valid.
Can anyone help me on how to integrate result of result variable into execution of reader? Can anyone help me please!?Any help/guidance in solving this problem would be greatly appreciated.
vishal