| 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รู้สึกว่าผมจะตอบคำถามเกี่ยวกับเรื่องนี้ไปแล้วนะครับ อีกครั้งก็ได้ครับ 
 Code (VB.NET)
 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sqlcon(1) '' ต่อ connection
        sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
        sb.Append("Select PromotionID ,CreateDate From Promotion")
        sb.Append(" Where  (CreateDate Between '" & DateTimePicker1.Value.ToString("MM/dd/yyy") & "' And '" & DateTimePicker2.Value.ToString("MM/dd/yyy") & "')")
        sql = sb.ToString() '' เก็บใน String 
        pro = Showquerysql(sql, "dtDetailPromotion") '' ยัดข้อมูลเข้า ใน datatable
        With DataGridView1 '' โชว์ข้อมูลใน datagridveiw
            .DataSource = pro
        End With
    End Sub
 ผิดพลาดประการใดขอ อภัย ด้วยครับ
 tee
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 09:49:30 | By :
                            lee_latee |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอโทษนะค่ะคุณ lee_latee  sb คือ ค่าอะไรค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 10:00:41 | By :
                            phangnga |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ค่า sb ทราบแล้วค่ะ แล้ว pro คืออะไรค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 10:21:22 | By :
                            phangnga |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เป็น StringBuilder ครับ เราประการตัวแปรไว้ที่ global ครับ เช่น Code (VB.NET)
 
 Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
public class Form1
dim con as sqlconnection
dim com as sqlcommand
dim sb as stringbuilder '' ตรงนี้ครับ
dim dtTable as datatable 
...
...
end class
 หรือจะสร้างเป็น Module เพื่อที่จะประการไว้ครั้งเดียวแล้วนำไปใช้งานได้ทุกฟอร์มครับ เช่น
 Code (VB.NET)
 
 Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
Module   moduleSql
 Public com As New SqlCommand
 Public con As New SqlConnection
 Public sb As New StringBuilder
 Public dtTabel as Datable
 Public sql As String
...
...
...
end module
 ไม่เข้าใจ ถามได้ตลอดนะครับ
 tee
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 10:43:53 | By :
                            lee_latee |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | pro คือ datable ครับ ประกาศเป็น ตัวแปรเหมือนการครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 10:45:41 | By :
                            lee_latee |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รับค่าจากการดึง db แล้วมาโชว์ใน grid ค่ะ จากนั้นก็ให้ทำการรวมค่าในคอลัมน์ รวมเป็นเงิน แล้วโชว์ข้อมูลที่ textbox ยังไงก็ช่วยหน่อยน่ะค่ะ ขอบคุณค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 10:47:10 | By :
                            a |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ค่า sb ได้แล้วค่ะ ขอโทษนะค่ะคุณ tee ตอนนี้ได้แล้วค่ะติดที่ showquerysql นะค่ะ รบกวนหน่อยนะค่ะ ขอบคุณค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 10:47:21 | By :
                            phangnga |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เอาใหม่ครับ Code (VB.NET)
 
 Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class Form2
    Dim dr As SqlDataReader
    Dim cn As SqlConnection
    Dim cm As SqlCommand
    Dim sb As New StringBuilder
    Dim strSql As String
    Dim strCn As String = "Data Source=vss-server;Initial Catalog=Sale;Integrated Security=True"
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn = New SqlConnection()
        With cn
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = strCn '' string ที่ใช้ต่อ connection
            .Open()
        End With
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
        sb.Append("Select PromotionID ,CreateDate From Promotion")
        sb.Append(" Where  (CreateDate Between '" & DateTimePicker1.Value.ToString("MM/dd/yyy") & "' And '" & DateTimePicker2.Value.ToString("MM/dd/yyy") & "')")
        strSql = sb.ToString() '' เก็บใน String 
        com = New SqlCommand()
        With com
            .CommandText = strSql
            .CommandType = CommandType.Text
            .Connection = cn
            dr = .ExecuteReader()
        End With
        If dr.HasRows Then ''check dr
            Dim dtSql As DataTable
            dtSql = New DataTable()
            dtSql.Load(dr)
            With DataGridView1
                .DataSource = dtSql
            End With
        End If
    End Sub
End Class
 อย่างนี้พอจะเข้าใจไหมครับ ถ้าไม่เข้าใจก็สอบถามใหม่ได้ครับ
 tee
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-27 17:19:29 | By :
                            lee_latee |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | สวัสดีค่ะคุณ lee_latee โค้ดที่คุณให้มาลองเอาไปทำดูแล้ว ไม่ error แต่ไม่มีข้อมูลอะไรโชว์ใน datagrid เลยค่ะ ไม่ทราบว่ามัน error อไรหรือเปล่าแต่มันไม่แจ้งออกมานะค่ะ  งงมากๆเลย 
 อือคุณ lee_latee ขอถามอะไรหน่อยนะค่ะ คือ จะให้ข้อมูลใน datagridview โชว์เฉพาะสินค้าที่ใกล้หมดอายุนะค่ะ แล้วเราจะเอาอะไรไปเทียบว่าสินค้าตัวนี้ใกล้หมดอายุ รบกวนหน่อยนะค่ะ ขอบคุณมากค่ะ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-28 00:21:41 | By :
                            phangnga |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | คำถามแรกที่ว่าข้อมูลไม่ออกผมก็งง เพราะผมทำก็ออกตามปกติ  อันนี้ขึ้นอยู่กับการเขียนโค้ด select ข้อมูลจาก DB ของคุณแล้วครับ  **แต่ผมยังงงทำไมไม่มีข้อมูล  ขอดูโค้ดในส่วนนี้หน่อย
 
 คำถามที่สอ รู้สึกว่าผมเคยตอบคำถามนี้ไปแล้วเกี่ยวกับสินค้าใกล้หมดอายุแล้วใน ใส่สีใน Grid
 ไม่เป็นไรเอาใหม่
 Code (VB.NET)
 
 Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
'Imports System.Windows.Forms
Public Class Form1
    Dim dr As SqlDataReader
    Dim cn As SqlConnection
    Dim cm As SqlCommand
    Dim sb As New StringBuilder
    Dim strSql As String
    Dim strCn As String = "Data Source=bkk314;Initial Catalog=DB_For_Webboard;Integrated Security=True"
    Dim dtTable As DataTable
    Dim Nowdate As Date
    Dim Enddate As Date
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connection()
        sb.Remove(0, sb.Length)
        sb.Append(" SELECT GETDATE() as Gdate") '' get วันที่ปัจจุบัน จาก server
        strSql = sb.ToString
        com = New SqlCommand()
        With com
            .CommandType = CommandType.Text
            .CommandText = strSql
            .Connection = cn
            dr = .ExecuteReader()
        End With
        If dr.HasRows Then
            dtTable = New DataTable
            dtTable.Load(dr)
            Nowdate = CDate(dtTable.Rows(0).Item("Gdate")).ToString("dd/MM/yyyy") '' DateNow
            Enddate = Nowdate.AddDays(7) '' บวกวัน ไปอีก 1 อาทิตย์ เพื่อเทียบวันใกล้หมดอายุ
            '**ถ้าอยู่ในช่วงวันที่ปัจจุบันและนับไปอีก 7 วัน ถือว่าใกล้หมดอายุ
        End If
        connection()
        sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
        sb.Append("Select productid,productname,price,qty,totalprice,datesales From Product")
        sb.Append(" Where (datesales between '" & Nowdate.ToString("MM/dd/yyy") & "' And ('" & Enddate.ToString("MM/dd/yyy") & "'))")
        strSql = sb.ToString()
        com = New SqlCommand()
        With com
            .CommandType = CommandType.Text
            .CommandText = strSql
            .Connection = cn
            dr = .ExecuteReader()
        End With
        If dr.HasRows Then
            dtTable = New DataTable
            dtTable.Load(dr)
            With DataGridView1
                .DataSource = dtTable
            End With
        End If
    End Sub
    Private Sub connection()
        cn = New SqlConnection()
        With cn
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = strCn '' string ที่ใช้ต่อ connection
            .Open()
        End With
    End Sub
End Class
 
  
 ไม่รู้ว่าตรงประเด็นหรือเปล่า
 tee
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-28 11:43:19 | By :
                            lee_latee |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | คุณ lee_latee  นี่เก่งกี่ภาษาครับเนี๊ย เห็นตอบตลอดเลยครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-28 12:53:04 | By :
                            zerocool |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | สวัสดีค่ะคุณ lee_latee โค้ดสินค้าใกล้หมดอายุลองเอาไปทำดูแล้ว แต่ว่ามันออกมาทั้งหมดเลยค่ะ ยังงัยก็ช่วยดูโค้ดให้หน่อยนะค่ะ  รบกวนหน่อยนะค่ะ ขอบคุณมากค่ะ 
 Imports System.Data
 Imports System.Data.SqlClient
 Imports System.Text
 Imports System.Windows.Forms
 
 Public Class FormReportExp
 Dim dr As SqlDataReader
 Dim cn As SqlConnection
 Dim cm As SqlCommand
 Dim sb As New StringBuilder
 Dim strSql As String
 Dim strCn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
 Dim dtTable As DataTable
 Dim Nowdate As DateTime
 Dim Enddate As DateTime
 
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
 If MessageBox.Show("ต้องการออกจากรายงานสินค้าใกล้หมดอายุใช่หรือไม่", "ออกจากระบบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
 Me.Close()
 End If
 End Sub
 
 Private Sub FormReportExp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
 Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
 Dim conn As New SqlConnection(conStr)
 conn.Open()
 Dim sql As String = "SELECT serialno,name,unit,totalstock,exp FROM product "
 Dim cmd As New SqlCommand(sql, conn)
 Dim adapter As New SqlDataAdapter(cmd)
 Dim data As New DataSet()
 adapter.Fill(data, "product")
 DataGridView1.DataSource = data.Tables("product")
 DataGridView1.Columns("serialno").HeaderText = "รหัสสินค้า"
 DataGridView1.Columns("name").HeaderText = "ชื่อสินค้า"
 DataGridView1.Columns("unit").HeaderText = "หน่วยสินค้า"
 DataGridView1.Columns("totalstock").HeaderText = "สินค้าคงคลัง"
 DataGridView1.Columns("exp").HeaderText = "วันหมดอายุ"
 conn.Close()
 End Sub
 
 Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
 connection()
 sb.Remove(0, sb.Length)
 sb.Append(" SELECT GETDATE() as Gdate") '' get วันที่ปัจจุบัน จาก server
 strSql = sb.ToString
 cm = New SqlCommand()
 With cm
 .CommandType = CommandType.Text
 .CommandText = strSql
 .Connection = cn
 dr = .ExecuteReader()
 End With
 If dr.HasRows Then
 dtTable = New DataTable
 dtTable.Load(dr)
 Nowdate = CDate(dtTable.Rows(0).Item("Gdate")).ToString("dd/MM/yyyy") '' DateNow
 Enddate = Nowdate.AddDays(7) '' บวกวัน ไปอีก 1 อาทิตย์ เพื่อเทียบวันใกล้หมดอายุ
 '**ถ้าอยู่ในช่วงวันที่ปัจจุบันและนับไปอีก 7 วัน ถือว่าใกล้หมดอายุ
 End If
 connection()
 sb.Remove(0, sb.Length) '' คำสั่ง Query ข้อมูล
 sb.Append("Select serialno,name,unit,totalstock,exp From product")
 sb.Append(" Where (exp between '" & Nowdate.ToString("dd/MM/yyy") & "' And ('" & Enddate.ToString("dd/MM/yyy") & "'))")
 strSql = sb.ToString()
 cm = New SqlCommand()
 With cm
 .CommandType = CommandType.Text
 .CommandText = strSql
 .Connection = cn
 dr = .ExecuteReader()
 End With
 If dr.HasRows Then
 dtTable = New DataTable
 dtTable.Load(dr)
 With DataGridView1
 .DataSource = dtTable
 End With
 End If
 End Sub
 Private Sub connection()
 cn = New SqlConnection()
 With cn
 If .State = ConnectionState.Open Then .Close()
 .ConnectionString = strCn '' string ที่ใช้ต่อ connection
 .Open()
 End With
 ' นับแถวของ datagridview
 order_total.Text = " " + DataGridView1.Rows.Count.ToString()
 End Sub
 End Class
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-28 16:29:08 | By :
                            phangnga |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอโค้ด datetimepicker แสดงรายงานC# หน่อยครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-07-15 16:31:23 | By :
                            ghostman19 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |