Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > รบกวนผู้รู้หน่อยครับ โค้ดที่ผมเขียนมา มันเกิด Error ที่ .ExcuteNonQuery() .. The transaction is either not associated with the current connection or has been completed


 

[.NET] รบกวนผู้รู้หน่อยครับ โค้ดที่ผมเขียนมา มันเกิด Error ที่ .ExcuteNonQuery() .. The transaction is either not associated with the current connection or has been completed

 
Topic : 078098



โพสกระทู้ ( 35 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Facebook


Code
The transaction is either not associated with the current connection or has been completed


Error

ตามรูปเลยครับ ไม่ทราบว่า ผมควรจะแก้ยังไงดี



Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2010 (.NET 4.x)

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-05-07 15:02:22 By : millanium2 View : 2198 Reply : 7
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เอา Code มาดูครับ เหมือนกับการการประกาศ Transaction และ เรียกใช้งาน Transaction ไม่ถุกต้องครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-07 15:13:44 By : mr.win
 

 

No. 2



โพสกระทู้ ( 35 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Facebook

อันนี้เป็นโค้ดในหน้าฟอร์มที่ผมใช้
Code (VB.NET)
001.Option Explicit On
002.Imports System.Text
003.Imports System.Data
004.Imports System.Data.SqlClient
005. 
006.Public Class frmCustomer
007.    ' Private Const conStr As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
008.    Private ReadOnly Property Conn() As SqlConnection
009.        Get
010.            Dim ConnToFetch As New SqlConnection(strCon)
011.            ConnToFetch.Open()
012.            Return ConnToFetch
013.        End Get
014.    End Property
015. 
016.    Public Function GetData() As DataView
017.        Dim sqlQry = "select * from Customer"
018.        Dim ds As New DataSet
019.        Dim dv As DataView
020.        Try
021.            Dim Comm As New SqlCommand()
022.            Dim da = New SqlDataAdapter()
023.            With Comm
024.                .CommandText = sqlQry
025.                .Connection = Conn
026.            End With
027.            da.SelectCommand = Comm
028.            da.Fill(ds)
029.            dv = ds.Tables(0).DefaultView
030.        Catch ex As Exception
031.            Throw ex
032.        End Try
033.        Return dv
034.    End Function
035. 
036.    Private Sub ShowColumnsHeads()
037.        If dgvCustomer.RowCount > 0 Then
038.            With dgvCustomer
039.                .Columns(0).HeaderText = "รหัสลูกค้า"
040.                .Columns(1).HeaderText = "ชื่อ"
041.                .Columns(2).HeaderText = "นามสกุล"
042.                .Columns(3).HeaderText = "ที่อยู่"
043.                .Columns(4).HeaderText = "หมายเลขโทรศัพท์"
044.                .Columns(5).HeaderText = "E-mail"
045.                .Columns(0).Width = 100
046.                .Columns(1).Width = 120
047.                .Columns(2).Width = 150
048.                .Columns(3).Width = 250
049.                .Columns(4).Width = 120
050.                .Columns(5).Width = 120
051. 
052.            End With
053.        End If
054.    End Sub
055.    Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
056.        dgvCustomer.DataSource = GetData()
057.        ShowColumnsHeads()
058.    End Sub
059. 
060.    Private Sub dgvCustomer_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvCustomer.CellMouseUp
061.        If e.RowIndex = -1 Then Exit Sub
062.        With dgvCustomer
063.            txtCusID.Text = .Rows.Item(e.RowIndex).Cells("CusID").Value.ToString()
064.            txtCusName.Text = .Rows.Item(e.RowIndex).Cells("CusName").Value.ToString()
065.            txtCusLastName.Text = .Rows.Item(e.RowIndex).Cells("CusLastName").Value.ToString()
066.            txtCusAddress.Text = .Rows.Item(e.RowIndex).Cells("CusAddress").Value.ToString()
067.            txtCusPhone.Text = .Rows.Item(e.RowIndex).Cells("CusPhone").Value.ToString()
068.            txtCusEmail.Text = .Rows.Item(e.RowIndex).Cells("CusEmail").Value.ToString()
069.            txtCusName.Focus()
070.            txtCusName.SelectAll()
071.        End With
072. 
073.    End Sub
074. 
075.    Sub AutoID()
076. 
077. 
078.        Dim tmp As Integer
079.        Sql = "SELECT Max(CusID) From Customer"
080.        Try
081.            Call dbOpen()
082.            Com = New SqlCommand(Sql, Conn)
083.            reader = Com.ExecuteReader()
084.            tmp = reader.FieldCount - 1
085. 
086.            If tmp < 0 Then
087.                txtCusID.Text = "00001"
088.            Else
089.                While reader.Read
090.                    txtCusID.Text = ((CInt((reader(0)).ToString.Substring(1))) + 1).ToString("00000")
091.                End While
092.            End If
093.        Catch ex As Exception
094.            MsgBox(ex.Message)
095.            MsgBox("Error Auto ID")
096.        End Try
097.    End Sub
098. 
099.    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
100.        Call AutoID()
101.        txtCusName.Focus()
102.    End Sub
103. 
104.    Private Sub InsertData()
105.        If MessageBox.Show("คุณต้องการเพิ่มข้อมูลลูกค้าใช่หรืไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
106.            tr = Conn.BeginTransaction
107. 
108.            With sb
109.                .Remove(0, sb.Length)
110.                .Append("insert into Customer (CusID,CusName,CusLastName,CusAddress,CusPhone,CusEmail)")
111.                .Append("values (@CusID,@CusName,@CusLastName,@CusAddress,@CusPhone,@CusEmail)")
112.                Dim sqlAdd As String
113.                sqlAdd = sb.ToString()
114. 
115.                With Com
116.                    .CommandText = sqlAdd
117.                    .CommandType = CommandType.Text
118.                    .Connection = Conn
119.                    .Transaction = tr
120.                    .Parameters.Clear()
121.                    .Parameters.Add("@CusID", SqlDbType.Char).Value = txtCusID.Text.Trim()
122.                    .Parameters.Add("@CusName", SqlDbType.VarChar).Value = txtCusName.Text.Trim()
123.                    .Parameters.Add("@CusLastName", SqlDbType.VarChar).Value = txtCusLastName.Text.Trim()
124.                    .Parameters.Add("@CusAddress", SqlDbType.VarChar).Value = txtCusAddress.Text.Trim()
125.                    .Parameters.Add("@CusPhone", SqlDbType.VarChar).Value = txtCusPhone.Text.Trim()
126.                    .Parameters.Add("@CusEmail", SqlDbType.VarChar).Value = txtCusEmail.Text.Trim()
127.                    .ExecuteNonQuery()
128.                End With
129.            End With
130.        End If
131.    End Sub
132. 
133.    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
134.        Call InsertData()
135.    End Sub
136.End Class


อันนี้เป็นโค้ดใน Module ครับ
Code (VB.NET)
01.Option Explicit On
02.Imports System.Data
03.Imports System.Data.SqlClient
04.Imports System.Transactions
05.Imports System.Text
06. 
07.Module Module1
08.    Public Conn As New SqlConnection()
09.    Public Com As New SqlCommand()
10.    Public strCon As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
11.    Public ds As New DataSet()
12.    Public sb As New StringBuilder()
13.    Public Sql As String
14.    Public reader As SqlDataReader
15.    Public tr As SqlTransaction
16. 
17.    Public Sub dbOpen()
18.        With Conn
19.            If .State = ConnectionState.Open Then .Close()
20.            .ConnectionString = strCon
21.            .Open()
22.        End With
23.    End Sub
24.End Module

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-07 15:23:12 By : millanium2
 

 

No. 3



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


รู้สึกว่าต้อง ประกาศ SqlTransaction ไว้ในคลาสที่เราจะใช้นะคับ ยังไงลองแก้ดู และเพื่อความชัวถ์ รอให้ พี่วิน มาตอบคับ

Code (VB.NET)
1.Dim tr As SqlTransaction 'ประกาศ
2. tr = Conn.BeginTransaction() 'เรียกใช้



ประวัติการแก้ไข
2012-05-07 15:34:05
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-07 15:33:26 By : slipknot1256
 

 

No. 4



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (VB.NET)
01.' Private Const conStr As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
02.Dim ConnToFetch As SqlConnection
03. 
04.Private ReadOnly Property Conn() As SqlConnection
05.    Get
06.        ConnToFetch = New SqlConnection(strCon)
07.        ConnToFetch.Open()
08.        Return ConnToFetch
09.    End Get
10.End Property


ลองดูแบบนี้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-07 20:27:59 By : mr.win
 

 

No. 5



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ได้ไม่ได้ยังไงบอกด้วยครับ อยากรู้เหมือนกัน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-07 22:36:16 By : mr.win
 

 

No. 6



โพสกระทู้ ( 35 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Facebook

ขอบคุณครับ ใช้ได้แล้ว
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-07 23:27:37 By : millanium2
 

 

No. 7



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เยี่ยม
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-05-08 05:51:04 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : รบกวนผู้รู้หน่อยครับ โค้ดที่ผมเขียนมา มันเกิด Error ที่ .ExcuteNonQuery() .. The transaction is either not associated with the current connection or has been completed
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่