using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.VisualBasic;
namespace student
{
public partial class UserReport : Form
{
ClassConnectDB Connect = new ClassConnectDB();
public UserReport()
{
InitializeComponent();
}
private void btnReport_Click(object sender, EventArgs e)
{
Connect.ConnectDB();//ติดต่ฐานข้อมูล
String strSQL;
DataTable dt = null;
strSQL = "SELECT user_id,user_name,user_address,user_province";
strSQL += ",user_zipcode,user_mail,user_phone FROM user where user_id like '%" + txtId.Text + "%' ";
MySqlDataAdapter adapter;
MySqlCommand objCmd;
objCmd = new MySqlCommand(strSQL, Connect.objConn);//ประมวลผล
adapter = new MySqlDataAdapter(objCmd);//เก็บข้อมูลไว้ที่ data adapter
DataSet data = new DataSet();//สร้าง data set
adapter.Fill(data, "user");//เก็บข้อมูลไว้ใน data set
dt = data.Tables[0];
adapter = null;
Connect.DisConnectDB();//ปิดฐานข้อมูล
Connect.objConn = null;//กำหนดค่าว่างให้ตัวแปร objConn
ReportDocument rpt = new ReportDocument();
string directory = AppDomain.CurrentDomain.BaseDirectory;
//use using System.IO;
//string directory = Path.GetDirectoryName(Application.ExecutablePath);
rpt.Load(directory +"..\\..\\UserCrystalReport.rpt");
// rpt.Load("E:\\BCShop\\student\\Project\\student\\UserCrystalReport.rpt");
rpt.SetDataSource(dt);
this.crystalReportViewer1.ReportSource = rpt;
this.crystalReportViewer1.Refresh();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.VisualBasic;
using CrystalDecisions.Shared;
using System.Drawing.Printing;
namespace student
{
public partial class UserReport : Form
{
ClassConnectDB Connect = new ClassConnectDB();
public UserReport()
{
InitializeComponent();
}
private void btnReport_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Connect.ConnectDB();//ติดต่ฐานข้อมูล
String strSQL;
DataTable dt = null;
strSQL = "SELECT user_id,user_name,user_address,user_province";
strSQL += ",user_zipcode,user_mail,user_phone FROM user where user_id like '%" + txtId.Text + "%' ";
MySqlDataAdapter adapter;
MySqlCommand objCmd;
objCmd = new MySqlCommand(strSQL, Connect.objConn);//ประมวลผล
adapter = new MySqlDataAdapter(objCmd);//เก็บข้อมูลไว้ที่ data adapter
DataSet data = new DataSet();//สร้าง data set
adapter.Fill(data, "user");//เก็บข้อมูลไว้ใน data set
dt = data.Tables[0];
adapter = null;
Connect.DisConnectDB();//ปิดฐานข้อมูล
Connect.objConn = null;//กำหนดค่าว่างให้ตัวแปร objConn
ReportDocument rpt = new ReportDocument();
string directory = AppDomain.CurrentDomain.BaseDirectory;
rpt.Load(directory + "..\\..\\UserCrystalReport.rpt");
rpt.SetDataSource(dt);
}
}
}
ถ้าผมทำตามคำแนะนำด้านมันเออเรอว่า The RPC server is unavailable ผมลองไปค้นหาเค้าบอกว่าให้ไป start service RPC แต่ก็ยังไม่ได้ครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.VisualBasic;
using CrystalDecisions.Shared;
using System.Drawing.Printing;
namespace student
{
public partial class UserReport : Form
{
ClassConnectDB Connect = new ClassConnectDB();
public UserReport()
{
InitializeComponent();
}
private void btnReport_Click(object sender, EventArgs e)
{
Connect.ConnectDB();//ติดต่ฐานข้อมูล
String strSQL;
DataTable dt = null;
strSQL = "SELECT user_id,user_name,user_address,user_province";
strSQL += ",user_zipcode,user_mail,user_phone FROM user where user_id like '%" + txtId.Text + "%' ";
MySqlDataAdapter adapter;
MySqlCommand objCmd;
objCmd = new MySqlCommand(strSQL, Connect.objConn);//ประมวลผล
adapter = new MySqlDataAdapter(objCmd);//เก็บข้อมูลไว้ที่ data adapter
DataSet data = new DataSet();//สร้าง data set
adapter.Fill(data, "user");//เก็บข้อมูลไว้ใน data set
dt = data.Tables[0];
adapter = null;
Connect.DisConnectDB();//ปิดฐานข้อมูล
Connect.objConn = null;//กำหนดค่าว่างให้ตัวแปร objConn
ReportDocument rpt = new ReportDocument();
string directory = AppDomain.CurrentDomain.BaseDirectory;
rpt.Load(directory + "..\\..\\UserCrystalReport.rpt");
rpt.SetDataSource(dt);
rpt.PrintToPrinter(1, false, 0, 0);
}
}
}