|  | 
	                
  
    |  |  
    | 
        
        (**ปัญหา**) บันทึกข้อมูลจากดาต้ากริดวิว และเท็กซ์บ๊อกซ์ลงฐานข้อมูลไม่ได้ค่ะ ช่วยด้วย !!! ขอบคุณค่ะ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | สวัสดีค่ะ พี่ ๆ คะขอความช่วยเหลือด้วยค่ะ T^T 
 คือต้องการบันทึกข้อมูล ลงตารางหนึ่งตารางค่ะ
 โดยที่ ต้องการข้อมูล จาก textbox อันนึง
 แล้วก็ต้องการข้อมูลจาก 1 คอลัมของ datagridview (คอลัมแรกค่ะ)
 แล้วกดปุ่มบันทึกข้อมูลค่ะ ให้บันทึกข้อมูลลงสองฟิลของตารางในฐานข้อมูล
 
 ตอนแรก หนูใช้โค้ดนี้ค่ะ
 แต่พอใช้แล้ว มันสามารถบันทึกข้อมูลได้นะคะ
 แต่ว่าพอโชว์เมสเสจ บันทึกข้อมูลเรียบร้อยแล้วให้เรากดโอเค
 เหมือนว่า มันเกิดการทำซ้ำไปอีกเรื่อย ๆ ค่ะ แล้วก็บันทึกข้อมูลมั่วไปหมดเลยค่ะ
 
 Code (VB.NET)
 
 For Each Dgv As DataGridViewRow In Me.DataGridView1.Rows
            Dim cn As New SqlConnection()
            If cn.State = ConnectionState.Open Then cn.Close()
            cn.ConnectionString = Form1.ConString
            cn.Open()
            Dim cm As New SqlCommand
            Dim sql As String = ""
            sql = "insert into SelectCourse (CustomerID, CourseID) values (@CustomerID, @CourseID)"
            cm.CommandText = sql
            cm.CommandType = CommandType.Text
            cm.Connection = cn
            cm.Parameters.Clear()
            cm.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 15).Value = TxtCusSearch.Text
            cm.Parameters.Add("@CourseID", SqlDbType.NChar, 8).Value = Dgv.Cells("CourseID").Value
            cm.ExecuteNonQuery()
            MsgBox("บันทึกข้อการจองคอร์สเรียบร้อยแล้ว")
        Next
 
 
 หนูก็เลยเปลี่ยนมาใช้โค้ดด้านล่างนี้ แต่ว่าพอใส่โค้ดนี้ แล้วมันเกิดเออเร่อขึ้นมาค่ะ
 
 Code (VB.NET)
 
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cn As New SqlConnection()
        If cn.State = ConnectionState.Open Then cn.Close()
        cn.ConnectionString = Form1.ConString
        cn.Open()
        Dim cm As New SqlCommand
        Dim sql As String = ""
        sql = "insert into SelectCourse (CustomerID, CourseID) values (@CustomerID, @CourseID)"
        cm.CommandText = sql
        cm.CommandType = CommandType.Text
        cm.Connection = cn
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            cm.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 15).Value = TxtCusSearch.Text
            cm.Parameters.Add("@CourseID", SqlDbType.NChar, 8).Value = DataGridView1.Rows(i).Cells(0).Value
            cm.ExecuteNonQuery()
        Next
    End Sub
 เออเร่อแจ้งว่าแบบนี้ค่ะ
 v
 
  
 พี่ ๆ ช่วยหนูด้วยค่ะ ควรแก้จากโค้ดไหน ยังไงดีคะ
 รบกวนพี่ ๆ อีกแล้ว ช่วยทีนะคะ
 ขอบคุณมากค่ะ
 
 
 
 Tag : - - - -
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2010-05-17 18:46:34 | By :
                          lovelydays | View :
                          1508 | Reply :
                          7 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (VB.NET) 
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
   Dim cn As New SqlConnection(Form1.ConString)
   'Dim tr As New SqlTransaction()
   Dim tr As SqlTransaction
   Dim success As Boolean = True;
   Dim errorMessage As String = String.Empty
   If cn.State <> ConnectionState.Open Then cn.Open()
   tr = cn.BeginTransaction(IsolationLevel.ReadCommitted)
   
   For i As Integer = 0 To DataGridView1.Rows.Count - 1
      Dim sql As String = "INSERT INTO [SelectCourse] ([CustomerID], [CourseID]) VALUES (@CustomerID, @CourseID)"
      Dim cm As New SqlCommand(sql, cn)
      cm.Parameters.AddWithValue("@CustomerID", TxtCusSearch.Text)
      cm.Parameters.AddWithValue("@CourseID", DataGridView1.Rows(i).Cells(0).Value)
      cm.Transaction = tr;
      
      Try
         cm.ExecuteNonQuery()
      Catch ex As Exception
         success = False
         errorMessage = ex.Message
         Exit For
      End Try
   Next
   If success Then
      tr.Commit()
      cn.Close()
      MsgBox("บันทึกข้อมูลการจองคอร์สเรียบร้อยแล้ว")
   Else
      tr.Rollback()
      cn.Close()
      MsgBox("เกิดข้อผิดพลาดไม่สามารถจองคอร์สได้ จากสาเหตุ: " & errorMessage)
   End If
End Sub
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-05-17 19:53:16 | By :
                            tungman |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณมากนะคะ แต่ยังติดอยู่เลยค่ะ ขอถามอีกรอบนะคะ
 
 ติดบรรทัดนี้ค่ะ
 
 Code (VB.NET)
 
 Dim tr As New SqlTransaction() Error :: Type 'System.Data.SqlClient.SqlTransaction' has no constructors.
 
 ขอบคุณค่ะ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-05-17 20:11:29 | By :
                            lovelydays |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | อิอิ ลืม เมามันไปหน่อย แก้แล้วไปดูที่ edit 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-05-17 20:17:52 | By :
                            tungman |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ยังบันทึกไม่ได้อยู่ดีค่ะ แง ๆๆๆๆ เมสเสจบ๊อกซ์แจ้งแบบนี้ค่ะ
 
 คือว่า หนูกำหนดอะไรผิดหรือป่าวคะ ??
 
 
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-05-17 20:35:52 | By :
                            lovelydays |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ตามไปศึกษาก่อนนะคะ ขอบคุณพี่มากค่ะ ^^
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-05-17 21:20:16 | By :
                            lovelydays |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ได้แล้วเรียบร้อยค่ะ ขอบคุณจริง ๆ ค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-05-18 03:32:30 | By :
                            lovelydays |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |