putu saputra

putu saputra

  • NA
  • 1
  • 6.6k

[C#] An error occured during local report processing

Jun 20 2014 6:55 AM
I want render this report and print it on runtime without report viewer. I try this code and i have exception. I am using datatable as a datasource. The exception said that report.Render("Image", deviceInfo, CreateStream, out warnings); is an error.
 
here is the code :
 
code 1 :
 
private void buttonCetakStruk_Click(object sender, EventArgs e)
{
cp = new c_penjualan();
cp.id_penjualan = id_penjualan1;
string tahun = tanggal1.Substring(6, 4);
string bulan = tanggal1.Substring(3, 2);
string tgl = tanggal1.Substring(0, 2);
cp.kasir = namakasir1;
cp.tanggal = tahun + bulan + tgl;
cp.nama_pembeli = namapembeli1;
cp.status = status1;
cp.diskon=decimal.Parse(textBoxDiskon.Text);
cp.grandtotal = decimal.Parse(labelGrandtotal.Text);
if(cp.insertPenjualan())
{
cdp= new c_detpenjualan();
d_udtoyaningDataSet isi = new d_udtoyaningDataSet();
ArrayList id_penj = new ArrayList();
ArrayList nmkasir = new ArrayList();
ArrayList tglstruk = new ArrayList();
ArrayList nmpembeli = new ArrayList();
ArrayList stts = new ArrayList();
ArrayList diskn = new ArrayList();
ArrayList grtotal = new ArrayList();
ArrayList nmbarang = new ArrayList();
ArrayList satun = new ArrayList();
ArrayList qt = new ArrayList();
ArrayList hrg_satuan = new ArrayList();
ArrayList jumlh = new ArrayList();
ArrayList bayr = new ArrayList();
for(int i=0;i<nama_barang1.Count;i++)
{
cdp.id_penjualan=id_penjualan1;
cdp.nama_barang=nama_barang1[i].ToString();
cdp.satuan=satuan1[i].ToString();
cdp.qty=int.Parse(qty1[i].ToString());
cdp.harga_satuan=decimal.Parse(harga_satuan1[i].ToString());
cdp.jumlah=decimal.Parse(jumlah1[i].ToString());
cdp.insertDetPenjualan();

id_penj.Add(id_penjualan1);
nmkasir.Add(namakasir1);
tglstruk.Add(tanggal1);
nmpembeli.Add(namapembeli1);
stts.Add(status1);
diskn.Add(decimal.Parse(textBoxDiskon.Text));
grtotal.Add(grandtotal1);
nmbarang.Add(nama_barang1[i].ToString());
satun.Add(satuan1[i].ToString());
qt.Add(int.Parse(qty1[i].ToString()));
hrg_satuan.Add(decimal.Parse(harga_satuan1[i].ToString()));
jumlh.Add(decimal.Parse(jumlah1[i].ToString()));
bayr.Add(decimal.Parse(textBoxBayar.Text));


}

ces = new cetakStruk();
ces.id_penj=id_penj;
ces.nmkasir=nmkasir;
ces.tglstruk=tglstruk;
ces.nmpembeli=nmpembeli;
ces.stts=stts;
ces.diskn=diskn;
ces.grtotal=grtotal;
ces.nmbarang=nmbarang;
ces.satun=satun;
ces.qt=qt;
ces.hrg_satuan=hrg_satuan;
ces.jumlh=jumlh;
ces.bayr=bayr;
ces.Run();
ac.Text = cp.nomor();
ab.Rows.Clear();
np1.Clear();
na1.ResetText();
na1.Text = "Cash";
MessageBox.Show("Data berhasil disimpan!");
this.Close();
}
else
{
MessageBox.Show("Data gagal disimpan!");
}
 
 code 2 :
 
 using System;
using System.IO;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;


namespace programPenjualan
{
class cetakStruk
{
private int m_currentPageIndex;
private IList<Stream> m_streams;

public ArrayList id_penj
{
get;
set;
}

public ArrayList nmkasir
{
get;
set;
}
public ArrayList tglstruk
{
get;
set;
}
public ArrayList nmpembeli
{
get;
set;
}
public ArrayList stts
{
get;
set;
}
public ArrayList diskn
{
get;
set;
}
public ArrayList grtotal
{
get;
set;
}
public ArrayList nmbarang
{
get;
set;
}
public ArrayList satun
{
get;
set;
}
public ArrayList qt
{
get;
set;
}
public ArrayList hrg_satuan
{
get;
set;
}
public ArrayList jumlh
{
get;
set;
}
public ArrayList bayr
{
get;
set;
}

private DataTable LoadSalesData()
{
// Create a new DataSet and read sales data file
// data.xml into the first DataTable.
DataTable apa = new DataTable();
//DataSet dataSet = new DataSet();
//dataSet.ReadXml(@"..\..\data.xml");
//dataSet.Tables[0].Rows[0].ItemArray[0]="ab";
apa.Columns.Add("id_penjualan");
apa.Columns.Add("kasir");
apa.Columns.Add("tanggal");
apa.Columns.Add("nama_pembeli");
apa.Columns.Add("status");
apa.Columns.Add("diskon");
apa.Columns.Add("grandtotal");
apa.Columns.Add("nama_barang");
apa.Columns.Add("satuan");
apa.Columns.Add("qty");
apa.Columns.Add("harga_satuan");
apa.Columns.Add("jumlah");
apa.Columns.Add("bayar");

for (int i = 0; i < this.id_penj.Count; i++)
{

apa.Rows.Add();
apa.Rows[i].ItemArray[0]=id_penj[i];
apa.Rows[i].ItemArray[1] = nmkasir[i];
apa.Rows[i].ItemArray[2] = tglstruk[i];
apa.Rows[i].ItemArray[3] = nmpembeli[i];
apa.Rows[i].ItemArray[4] = stts[i];
apa.Rows[i].ItemArray[5] = diskn[i];
apa.Rows[i].ItemArray[6] = grtotal[i];
apa.Rows[i].ItemArray[7] = nmbarang[i];
apa.Rows[i].ItemArray[8] = satun[i];
apa.Rows[i].ItemArray[9] = qt[i];
apa.Rows[i].ItemArray[10] = hrg_satuan[i];
apa.Rows[i].ItemArray[11] = jumlh[i];
apa.Rows[i].ItemArray[12] = bayr[i];

}
return apa;
//return dataSet.Tables[0];
}
// Routine to provide to the report renderer, in order to
// save an image for each page of the report.
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.25in</MarginTop>" +
" <MarginLeft>0.25in</MarginLeft>" +
" <MarginRight>0.25in</MarginRight>" +
" <MarginBottom>0.25in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
try
{
report.Render("Image", deviceInfo, CreateStream, out warnings);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
foreach (Stream stream in m_streams)
stream.Position = 0;

}
// Handler for PrintPageEvents
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);

// Adjust rectangular area with printer margins.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);

// Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);

// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);

// Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}

private void Print()
{
const string printerName = "EPSON TX121 Series";
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = printerName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("Can't find printer \"{0}\".", printerName);
MessageBox.Show(msg);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();

}
// Create a local report for Report.rdlc, load the data,
// export the report to an .emf file, and print it.
public void Run()
{
LocalReport report = new LocalReport();
report.ReportPath = "struk.rdlc";
report.DataSources.Add(
new ReportDataSource("apa", LoadSalesData()));
Export(report);
Print();
}

public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}

/*public static void Main(string[] args)
{
using (Demo demo = new Demo())
{
demo.Run();
}
}*/
}

}