|  |  | 
          
            | มีปัญหาเกี่ยวกับการ UPDATE ข้อมูลไปยัง MYSQL มัน Error ช่วยดูให้ทีครับ แต่ INSERT ข้อมูลไม่มีปัญหา 
 เวลา INSERT จะบันทึกข้อมูลได้ปกติ แต่เวลาจะ UPDATE ข้อมูล มัน error ตามนี้ครับ
 
 Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
 
 
 ... ผมใส่อะไรผิด
 
 ดูในตาราง SQL ก็ไม่น่าจะมีปัญหา
 
 
  
 
 อันนี้ code  UPDATE ข้อมูล
 
 Code (VB.NET)
 
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mysqlcon.Open()
        With mysqlcmd
            .Connection = mysqlcon
            .CommandText = "UPDATE data SET zzz1 = '" & text1.Text & "',zzz2 = '" & text2.Text & "',zzz3 = '" & text3.Text & "',zzz4 = '" & text4.Text & "',zzz5 = '" & text5.Text & "',zzz6 = '" & text6.Text & "',zzz7 = '" & text7.Text & "',zzz8 = '" & text8.Text & "',text9 = '" & text9.Text & "' WHERE id = " & DataGridView1.SelectedRows(0).Cells(0).Value
            i = .ExecuteNonQuery
        End With
        If i > 0 Then
            MsgBox("แก้ไขข้อมูล เรียบร้อยแล้ว", MsgBoxStyle.Information)
        End If
        mysqlcon.Close()
        showdata()
    End Sub
 
  
 
  
 
 อันนี้คือ code ทั้งหมด
 
 Code (VB.NET)
 
 Imports MySql.Data.MySqlClient
Imports MySql.Data.Types
Imports System.IO
Imports System.Net
Imports System.Data
Imports System
Public Class Form11
    Dim mysqlcon As New MySqlConnection("SERVER=127.0.0.1;USERID=root;PASSWORD=1;DATABASE=test;Character Set=utf8;")
    Dim mysqlcmd As New MySqlCommand
    Dim da As New MySqlDataAdapter
    Dim dt As New DataTable
    Dim i As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.RowTemplate.Height = 30
        showdata()
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        id.Text = DataGridView1.Item(0, e.RowIndex).Value
        text1.Text = DataGridView1.Item(1, e.RowIndex).Value
        text2.Text = DataGridView1.Item(2, e.RowIndex).Value
        text3.Text = DataGridView1.Item(3, e.RowIndex).Value
        text4.Text = DataGridView1.Item(4, e.RowIndex).Value
        text5.Text = DataGridView1.Item(5, e.RowIndex).Value
        text6.Text = DataGridView1.Item(6, e.RowIndex).Value
        text7.Text = DataGridView1.Item(7, e.RowIndex).Value
        text8.Text = DataGridView1.Item(8, e.RowIndex).Value
        text9.Text = DataGridView1.Item(9, e.RowIndex).Value
    End Sub
    Private Sub showdata()
        Dim cmd1 As New MySqlCommand
        Dim ada As MySqlDataAdapter
        Dim table As DataTable
        Try
            mysqlcon.Open()
            cmd1.Connection = mysqlcon
            ada = New MySqlDataAdapter("SELECT*FROM data", mysqlcon)
            table = New DataTable
            ada.Fill(table)
            DataGridView1.DataSource = table
            mysqlcon.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If MessageBox.Show("คุณต้องการบันทึกข้อมูลนี้", "ยืนยันการบันทึก", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
            mysqlcon.Open()
            With mysqlcmd
                .Connection = mysqlcon
                .CommandText = "INSERT INTO data(zzz1,zzz2,zzz3,zzz4,zzz5,zzz6,zzz7,zzz8,zzz9)VALUES('" & text1.Text & "','" & text2.Text & "','" & text3.Text & "','" & text4.Text & "','" & text5.Text & "','" & text6.Text & "','" & text7.Text & "','" & text8.Text & "','" & text9.Text & "')"
                i = .ExecuteNonQuery
            End With
            If i > 0 Then
                MsgBox("บันทึกข้อมูล เรียบร้อยแล้ว !", MsgBoxStyle.Information)
                reset()
            End If
            mysqlcon.Close()
            showdata()
        Else
            MsgBox("ยกเลิกการบันทึก", MsgBoxStyle.Exclamation)
        End If
    End Sub
    Private Sub Data_History_Bus_Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("คุณต้องการปิดหน้าต่างนี้", "ออกจากหน้าต่าง", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
            e.Cancel = False
        Else
            e.Cancel = True
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mysqlcon.Open()
        With mysqlcmd
            .Connection = mysqlcon
            .CommandText = "UPDATE data SET zzz1 = '" & text1.Text & "',zzz2 = '" & text2.Text & "',zzz3 = '" & text3.Text & "',zzz4 = '" & text4.Text & "',zzz5 = '" & text5.Text & "',zzz6 = '" & text6.Text & "',zzz7 = '" & text7.Text & "',zzz8 = '" & text8.Text & "',text9 = '" & text9.Text & "' WHERE id = " & DataGridView1.SelectedRows(0).Cells(0).Value
            i = .ExecuteNonQuery
        End With
        If i > 0 Then
            MsgBox("แก้ไขข้อมูล เรียบร้อยแล้ว", MsgBoxStyle.Information)
        End If
        mysqlcon.Close()
        showdata()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mysqlcon.Open()
        With mysqlcmd
            .Connection = mysqlcon
            .CommandText = "DELETE FROM data WHERE id = " & id.Text 'DataGridView1.SelectedRows(0).Cells(0).Value
            i = .ExecuteNonQuery
        End With
        If i > 0 Then
            MsgBox("ลบข้อมูล เรียบร้อยแล้ว", MsgBoxStyle.Information)
            reset()
        End If
        mysqlcon.Close()
        showdata()
    End Sub
    Private Sub reset()
        text1.Text = ""
        text2.Text = ""
        text3.Text = ""
        text4.Text = ""
        text5.Text = ""
        text6.Text = ""
        text7.Text = ""
        text8.Text = ""
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub
End Class
 
 
 
 อันนี้คือ VDO ที่ผมดู https://www.youtube.com/watch?v=_BA5IT_UU3A
 
 
 
 Tag : .NET, MySQL, VB.NET, VS 2008 (.NET 3.x), Windows
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2015-06-22 17:50:04 | By :
                          wanlop8822 | View :
                          893 | Reply :
                          5 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |