Pages

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

Monday, September 27, 2010

Use Thread update Text of TextBox

วันนี้ทดลองเขียนโปรแกรมเล่น ๆ ดูเกี่ยวกับ Threading ของ C# เพราะว่าจำเป็นต้องนำ thread ไปใช้งาน
เพื่อให้มันทำงานเป็น backgroupd ของ program เพื่อใช้ในการตรวจสอบว่าจำนวนที่เราเคยโหลดมาไว้ กับจำนวนที่เกิดขึ้นใหม่ เท่ากันหรือไม่ ซื้อทั้งสองจำนวนนี้อยู่คนละ DB กัน

มาเริ่มกันเลย ^^


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.Threading;  namespace WindowsFormsApplication1 {     public partial class Form1 : Form     {         Thread tWatchTime;         // delegate is used to communicate with UI Thread from a non-UI thread         public delegate void UpdateTextCallback(string message);         public Form1()         {             InitializeComponent();         }          private void Form1_Load(object sender, EventArgs e)         {                     }          private void timer1_Tick(object sender, EventArgs e)         {             textBox2.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":"                 + DateTime.Now.Second.ToString();         }          private void button1_Click(object sender, EventArgs e)         {             try             {                 if (button1.Text == "Start")                 {                     tWatchTime = new Thread(WatchTime);                     tWatchTime.IsBackground = true;                     tWatchTime.Start();                      button1.Text = "Stop";                 }                 else                 {                     tWatchTime.Abort();                     button1.Text = "Start";                 }             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }         }          public void WatchTime()         {             while (tWatchTime.IsAlive)             {                 Thread.Sleep(5000);                 textBox1.BeginInvoke(new UpdateTextCallback(UpdateText), new object[] { DateTime.Now.Second.ToString() });             }         }          public void UpdateText(string msg)         {             textBox1.Text = msg;         }          //textBox1.BeginInvoke(new UpdateTextCallback(UpdateText), new object[] { i.ToString() });         //    }         //}           //private void UpdateText(string message)         //{         //    textBox1.Text = message;         //}     } }

แต่ในตัวอย่างนี้ผมใช้ Thread ไป set time ทุก ๆ 5 วินาที ก็เลยต้องสั่งให้มัน sleep(5000)

หลังจากผ่านไป 5 วิทนาที ก็ set Textbox1.text ให้โดยดึงเอา second มาแสดง (แต่ในภาพอาจจะไม่ตรงเพราะจะมีเวลาหน่วงในการ save picture)



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

Thursday, August 5, 2010

DateTime Format

สำหรับนักพัฒนาโปรแกรมแล้ว เรื่อง Datetime ค่อนข้าวจะเรียกได้ว่าเป็นปัญหามากทีเดียว บางคนอาจสงสัยว่ามันเป็นปัญหายังไง สำหรับปัญหานี้จะไม่มีถ้าโปรแกรมเราทำงานเพียงเครื่องเดียวไม่ต้อง connect กับ database ที่อยู่ยังเครื่องอื่น เพราะถ้าเรา connect database เครื่องอื่นแล้ว datetime format ของเครื่งอแต่ละเครื่องอาจไม่เหมือนกัน สามารถทำได้โดยการ check ดูว่าเครื่องมี cultureinfo เป็นอะไรแล้วค่อยเปลี่ยน แต่จริง ๆ ควรเอา Datetime จากเครื่อง server database เลยจะดีที่สุด

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
ปัญหานี้ก็หมดไป

Monday, June 21, 2010

Unable to create a manifest resource name

Error 1 Unable to create a manifest resource name for "frmCheckOut.resx". Could not find file 'C:\Users\wirat\Documents\Visual Studio 2008\Projects\Inventory\Inventory\frmCheckOut.cs'. Inventory


เจอแบบนี้ไปไม่เป็นเลย อุตสาห์นั่งเขียนทั้งวันหายหมด ไม่รู้จะทำยังไงดี ลองเอา error ไปหาใน google ดูก็ไม่มีวิธีแก้ไข เพราะมันเป็น bug ของ vs 2008 ตายห่าเลยตู นั่งเขียนทั้งวัน

แต่ยังดีสามารถ recovery design page กลับคืนมาได้ แต่ code ต่างหายหมด - -"

วิธีก็คือ
  1. สร้าง new form เป็น form1.cs หละกัน ก็จะได้หน้า form มาเปล่าว ๆ ยังไม่มีอะไร จะเห็นมันมีอยู่ 2 ไฟล์ คือ form1.cs กับ form1.Designer.cs
  2. ลาก button หรืออะไรก็ได้มาใส่แล้วก็ save ทีนี้จะสังเกตเห็นเพิ่มมาอีก 1 ไฟล์ คือ form1.resx
  3. close solution
  4. rename form1.cs -> xxx.cs เพราะไฟล์ที่มันหายไปคือ xxx.cs เราจะสร้างอันใหม่ขึ้นมาแทนแต่จะยังได้ designer กลับคือ (ก็ยังดีวะ)
  5. เปิด xxx.cs แล้วมันจะฟ้อง Error ช่างหัวมันให้ ignore มันเลยแล้วก็จะได้หน้า designer กลับมา