  | 
              
	              
	                
  
    |   | 
   
  
    
        
        รบกวนผู้รู้หน่อยครับ โค้ดที่ผมเขียนมา มันเกิด Error ที่ .ExcuteNonQuery() .. The transaction is either not associated with the current connection or has been completed     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เอา Code มาดูครับ เหมือนกับการการประกาศ Transaction และ เรียกใช้งาน Transaction ไม่ถุกต้องครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-07 15:13:44 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 อันนี้เป็นโค้ดในหน้าฟอร์มที่ผมใช้ 
Code (VB.NET) 
Option Explicit On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Public Class frmCustomer
    ' Private Const conStr As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
    Private ReadOnly Property Conn() As SqlConnection
        Get
            Dim ConnToFetch As New SqlConnection(strCon)
            ConnToFetch.Open()
            Return ConnToFetch
        End Get
    End Property
    Public Function GetData() As DataView
        Dim sqlQry = "select * from Customer"
        Dim ds As New DataSet
        Dim dv As DataView
        Try
            Dim Comm As New SqlCommand()
            Dim da = New SqlDataAdapter()
            With Comm
                .CommandText = sqlQry
                .Connection = Conn
            End With
            da.SelectCommand = Comm
            da.Fill(ds)
            dv = ds.Tables(0).DefaultView
        Catch ex As Exception
            Throw ex
        End Try
        Return dv
    End Function
    Private Sub ShowColumnsHeads()
        If dgvCustomer.RowCount > 0 Then
            With dgvCustomer
                .Columns(0).HeaderText = "รหัสลูกค้า"
                .Columns(1).HeaderText = "ชื่อ"
                .Columns(2).HeaderText = "นามสกุล"
                .Columns(3).HeaderText = "ที่อยู่"
                .Columns(4).HeaderText = "หมายเลขโทรศัพท์"
                .Columns(5).HeaderText = "E-mail"
                .Columns(0).Width = 100
                .Columns(1).Width = 120
                .Columns(2).Width = 150
                .Columns(3).Width = 250
                .Columns(4).Width = 120
                .Columns(5).Width = 120
            End With
        End If
    End Sub
    Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvCustomer.DataSource = GetData()
        ShowColumnsHeads()
    End Sub
    Private Sub dgvCustomer_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvCustomer.CellMouseUp
        If e.RowIndex = -1 Then Exit Sub
        With dgvCustomer
            txtCusID.Text = .Rows.Item(e.RowIndex).Cells("CusID").Value.ToString()
            txtCusName.Text = .Rows.Item(e.RowIndex).Cells("CusName").Value.ToString()
            txtCusLastName.Text = .Rows.Item(e.RowIndex).Cells("CusLastName").Value.ToString()
            txtCusAddress.Text = .Rows.Item(e.RowIndex).Cells("CusAddress").Value.ToString()
            txtCusPhone.Text = .Rows.Item(e.RowIndex).Cells("CusPhone").Value.ToString()
            txtCusEmail.Text = .Rows.Item(e.RowIndex).Cells("CusEmail").Value.ToString()
            txtCusName.Focus()
            txtCusName.SelectAll()
        End With
    End Sub
    Sub AutoID()
        Dim tmp As Integer
        Sql = "SELECT Max(CusID) From Customer"
        Try
            Call dbOpen()
            Com = New SqlCommand(Sql, Conn)
            reader = Com.ExecuteReader()
            tmp = reader.FieldCount - 1
            If tmp < 0 Then
                txtCusID.Text = "00001"
            Else
                While reader.Read
                    txtCusID.Text = ((CInt((reader(0)).ToString.Substring(1))) + 1).ToString("00000")
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox("Error Auto ID")
        End Try
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Call AutoID()
        txtCusName.Focus()
    End Sub
    Private Sub InsertData()
        If MessageBox.Show("คุณต้องการเพิ่มข้อมูลลูกค้าใช่หรืไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            tr = Conn.BeginTransaction
            With sb
                .Remove(0, sb.Length)
                .Append("insert into Customer (CusID,CusName,CusLastName,CusAddress,CusPhone,CusEmail)")
                .Append("values (@CusID,@CusName,@CusLastName,@CusAddress,@CusPhone,@CusEmail)")
                Dim sqlAdd As String
                sqlAdd = sb.ToString()
                With Com
                    .CommandText = sqlAdd
                    .CommandType = CommandType.Text
                    .Connection = Conn
                    .Transaction = tr
                    .Parameters.Clear()
                    .Parameters.Add("@CusID", SqlDbType.Char).Value = txtCusID.Text.Trim()
                    .Parameters.Add("@CusName", SqlDbType.VarChar).Value = txtCusName.Text.Trim()
                    .Parameters.Add("@CusLastName", SqlDbType.VarChar).Value = txtCusLastName.Text.Trim()
                    .Parameters.Add("@CusAddress", SqlDbType.VarChar).Value = txtCusAddress.Text.Trim()
                    .Parameters.Add("@CusPhone", SqlDbType.VarChar).Value = txtCusPhone.Text.Trim()
                    .Parameters.Add("@CusEmail", SqlDbType.VarChar).Value = txtCusEmail.Text.Trim()
                    .ExecuteNonQuery()
                End With
            End With
        End If
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Call InsertData()
    End Sub
End Class
 
 
อันนี้เป็นโค้ดใน Module ครับ 
Code (VB.NET) 
Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Transactions
Imports System.Text
Module Module1
    Public Conn As New SqlConnection()
    Public Com As New SqlCommand()
    Public strCon As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
    Public ds As New DataSet()
    Public sb As New StringBuilder()
    Public Sql As String
    Public reader As SqlDataReader
    Public tr As SqlTransaction
    Public Sub dbOpen()
        With Conn
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = strCon
            .Open()
        End With
    End Sub
End Module
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-07 15:23:12 | 
                        By :
                            millanium2 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 รู้สึกว่าต้อง ประกาศ SqlTransaction ไว้ในคลาสที่เราจะใช้นะคับ ยังไงลองแก้ดู และเพื่อความชัวถ์ รอให้ พี่วิน มาตอบคับ   
 
Code (VB.NET) 
 Dim tr As SqlTransaction 'ประกาศ
  tr = Conn.BeginTransaction() 'เรียกใช้
                         
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2012-05-07 15:34:05              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-07 15:33:26 | 
                        By :
                            slipknot1256 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (VB.NET) 
    ' Private Const conStr As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
    Dim ConnToFetch As SqlConnection
    
    Private ReadOnly Property Conn() As SqlConnection
        Get
            ConnToFetch = New SqlConnection(strCon)
            ConnToFetch.Open()
            Return ConnToFetch
        End Get
    End Property
 
 
ลองดูแบบนี้ครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-07 20:27:59 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ได้ไม่ได้ยังไงบอกด้วยครับ อยากรู้เหมือนกัน                          
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-07 22:36:16 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณครับ ใช้ได้แล้ว                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-07 23:27:37 | 
                        By :
                            millanium2 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   เยี่ยม                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-05-08 05:51:04 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |