Pages

Showing posts with label C#. Show all posts
Showing posts with label C#. 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

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 แบบนี้เลย ทดลองดูตั้งนานกว่าจะได้ สุดท้ายไปค้นถึงขยะ เอาไฟล์กลับคืนมา ฮ่า.....

Thursday, August 5, 2010

DateTime Format

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

Monday, June 28, 2010

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

ซะเยอะเลย