Pages

Showing posts with label Visual Studio 2008. Show all posts
Showing posts with label Visual Studio 2008. Show all posts

Monday, August 30, 2010

Connection pool SQL Server

ทดลองเขียนโปรแกรม connection database โดยใช้ table adapter ดูแล้วง่ายมาก ๆ เลยครับ
ก็แค่ add data set เข้าไปแล้วก็สร้าง connection แล้ว add table , table adapter เข้าไปใน data set
แต่ทำไปทำมาเจอปัญหา เพราะว่า table adapter ของผมมันเยอะ ถึงขั้น ร้อยสองร้อยเลย ทำให้ visual studio 2008 ทำงานค่อยข้างช้าหลังจากที่ create table adapter แล้วต้องรอสักประมาณ 30 วินาทีถึงจะสามารถเขียนโปรแกรมต่อได้ เหอะๆ

จริงมันก็มีวิธีอีกอย่าง ในเมื่อ table adapter มันเยอะ ทำไมเราไม่แบ่งย่อย ๆ ออกไปโดยเพิ่ม data set เข้าไปหละ คำตอบนี้ค่อนข้างได้ผลทีเดียวครับ ผมลองแบ่งย่อยทำหลาย ๆ dataset ดู เจ้า vs 2008 มันก็ทำงานได้เร็วจึ้นกว่าเดิมเยอะเลย จากทีเคยรอ ก็ไม่ต้องรออีก แต่คำถามที่ตามมาคือ เมื่อสร้าง dataset หลาย ๆ ตัวแล้ว connection pool หละ มันจะเพิ่มขึ้นด้วยตามหรือเปล่าว จึงลองหาข้อมูลดู

ดู ๆ แล้วมันไม่น่าจะสร้าง connection เพิ่มนะ จาก link

Sunday, August 29, 2010

Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561

Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed


เจอ error แบบนี้แล้วงงเลยครับ ทำให้ผมเสียเวลาไปตั้งเกือบชั่วโมง สามาเหตุไม่ใช่เพราะอะไรหรอกครับ
ผมไปเลบไฟล์ app.minifast ที่อยู่ใน Properities ออกแล้วผมก็เอา code ไปเขียนที่เครื่องอื่นก็เลยเจอ Error แบบนี้เลย ทดลองดูตั้งนานกว่าจะได้ สุดท้ายไปค้นถึงขยะ เอาไฟล์กลับคืนมา ฮ่า.....

Tuesday, June 29, 2010

Report viewer Print without preview

เวลาที่ทำโปรแกรมสักอย่างจะต้องมีรายงานอาจจะมากหรือน้อยก็แล้วแต่ว่า user ต้องการ ปกติผมจะใช้ Report viewer ในการสร้างรายงาน เช่น ใบสั่งซื้อ ใบรายการสินค้าต่าง ๆ

ปกติถ้าเราใช้ Report viewer จะต้องลาง control มาว่างใน form แต่ทีนี้ผมต้องการแบบไม่ใช้ control วางใน form ให้สามารถกดปุ่มพิมพ์แล้วพิมพ์ได้เลยโดยไม่ต้องแสดง report viewer ก็

สำหรับวิธีการทำ

Tools ที่ผมใช้
  1. Visual Studio 2008
  2. MS SqlServer 2008
  3. Report viewer 2008 (ตัวนี้จะต้องตาม vs ที่เราใช้นะครับ)
  4. คอม - -" (ไม่ใช้คอมจะทำไงวะเนี่ย)
มาเริ่มกันเลย

  1. สร้าง Form ขึ้นมาสักอัน
  2. สร้าง dataset
  3. สร้าง Report.rdlc พร้อมทั้ง fomat ของข้อมูลที่ต้องการแสดง
  4. หลังจากที่เราได้ครบแล้วก็มาเขียน code กันเลย

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

ReportViewer ReportViewer1 = new ReportViewer();
byte[] bytes = null;
private void button1_Click(object sender, EventArgs e)
{
//this.dataSet11.Customers.WriteXml("data1.xml");
Run();
}

private void Form1_Load(object sender, EventArgs e)
{
customersTableAdapter1.Fill(this.dataSet1.Customers);
this.dataSet1.Customers.WriteXml("data1.xml");
}

private int m_currentPageIndex;

private IList m_streams;

private DataTable LoadSalesData()
{
DataSet dataSet = new DataSet();
dataSet.ReadXml("data1.xml");
return dataSet.Tables[0];
}

private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}

private void Export(LocalReport report)
{
string deviceInfo =
"" +
" EMF" +
" 8.5in" +
" 11in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
"";
Warning[] warnings;
m_streams = new List();
//report.Render("Image", deviceInfo, CreateStream, out warnings);
//Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

bytes = ReportViewer1.LocalReport.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(@"D:\output.PDF",
FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();


foreach (Stream stream in m_streams)
stream.Position = 0;
}

private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex <>
}

private void Print()
{
const string printerName = "Microsoft Office Document Image Writer";

if (m_streams == null || m_streams.Count == 0)
return;

PrintDocument printDoc = new PrintDocument();
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
printDialog.Document = printDoc;
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
return;

printDoc.PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("Can't find printer \"{0}\".", printerName);
Console.WriteLine(msg);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}

public void Run()
{
//LocalReport report = new LocalReport();
//report.ReportPath = "Report.rdlc";
//report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
//report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
//ReportViewer oViewer = new ReportViewer();

//this.customersTableAdapter1.Fill(this.dataSet1.Customers);
this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
this.ReportViewer1.LocalReport.ReportPath =
@"Report.rdlc";
ReportViewer1.LocalReport.DataSources.Add(
new ReportDataSource("Customers", LoadSalesData()));

string deviceInfo =
"" +
" EMF" +
" 8.5in" +
" 11in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
"";
Warning[] warnings;
//m_streams = new List();
//report.Render("Image", deviceInfo, CreateStream, out warnings);

//Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

bytes = ReportViewer1.LocalReport.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(@"D:\output.PDF",
FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();


foreach (Stream stream in m_streams)
stream.Position = 0;

//Export(report);

m_currentPageIndex = 0;
Print();
}

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

Resource : http://www.gotreportviewer.com/

Download

Monday, June 28, 2010

The CurrentContext property of the LicenseManager is currently locked and cannot be chaged

หลังจากที่ผม install add-on ของ vs2010 พอกลับมาเปิด solution ของ vs2008 กลับเจอ error แบบนี้ครับ
ปัญหาคือ ว่า add-on ที่ผมลงมันเป็นของ vs2010 มันไม่ support vs2008


วิธีแก้คือต้อง reset ค่าเดิมที่เคย setting ไว้แล้ว
ไปที่ Tools->Import and Export setting จากนั้นเลือกเป็น Reset all setting
ปัญหานี้ก็หมดไป

Print report without display c#

วันนี้พยายามที่จะสร้างใบส่งของ โดยที่เวลากดปุ่มแล้วให้มันพิมพ์รายการที่ต้องการส่ง

ปกติที่ผมเคยทำจะใช้ Reportviewer หรือไม่ก็ CrytalReport แต่ว่ามันจะต้องแสดงใน form ก็แล้วค่อยกดปริ้นทีหลัง

อีกวิธีคือ ใช้ Graphic.drawString เอาเลยแบบง่าย ๆ

แต่น่าจะมีวิธีที่ง่ายกว่านี้ก็คงต้องใช้ reportviewer ในการพิมพ์เลยแต่ไม่ต้องการให้มันแสดงใน form เนี่ยสิทำยังไงดี ก็เลยของหาใน google ไปเจอ tutorial ของ microsoftที่ http://msdn.microsoft.com/en-us/library/ms252091(VS.80).aspx แต่ก็ยังงงอยู่ดี เพราะตัว Report ที่ผมใช้เป็น version 2008 พอ build มันจะฟ้อง error

Error 2 The report definition is not valid. Details: The element 'ReportItems' in namespace 'http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition' has invalid child element 'Tablix' in namespace 'http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition'. List of possible elements expected: 'http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Line http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Rectangle http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Textbox http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Image http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Subreport http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:List http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Matrix http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Table http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:Chart http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition:CustomReportItem ##other:*'. C:\Users\wirat\Documents\Visual Studio 2008\Projects\Inventory\Inventory\Report1.rdlc Inventory

ซะเยอะเลย