'check for the selected item in list
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As string = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' ", con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = intsi
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
End If
End If
End If
End Sub
'get data into datatable
Dim strSQL As String = "SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' "
Dim da As New OleDbDataAdapter(strSQL,con)
Dim dt As New DataTable()
da.Fill(dt)
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway')
Dim custDA As OleDbDataAdapter = New OleDbDataAdapter()
Dim custDS As DataSet = New DataSet
Dim custTable As DataTable = New DataTable("Customers")
custTable.Columns.Add("CustomerID", Type.GetType("System.String"))
custTable.Columns.Add("CompanyName", Type.GetType("System.String"))
custDS.Tables.Add(custTable)
'Use ADO objects from ADO library (msado15.dll) imported
' as.NET library ADODB.dll using TlbImp.exe
Dim adoConn As ADODB.Connection = New ADODB.Connection()
Dim adoRS As ADODB.Recordset = New ADODB.Recordset()
adoConn.Open("Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;", "", "", -1)
adoRS.Open("SELECT CustomerID, CompanyName FROM Customers", adoConn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1)
custDA.Fill(custTable, adoRS)
adoRS.Close()
adoConn.Close()
จากตัวอย่าง ลองย้าย statement ใน da มาไว้ใน dt แทนครับ
Dim dt as Datatable = new Datatable( statment );
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim dt As DataTable = New DataTable("SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' ")
da.Fill(dt, con)
Public Function GetTableForm(connectionString As String, _SQL As String) As System.Data.DataTable
Dim ds As New System.Data.DataSet()
Dim conn1 As New System.Data.OleDb.OleDbConnection(connectionString)
conn1.Open()
Dim adapter1 As New System.Data.OleDb.OleDbDataAdapter(_SQL, conn1)
adapter1.Fill(ds)
Return ds.Tables(0)
End Function
Code (VB.NET)
Dim dt As System.Data.DataTable = GetTableForm("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=X:\DATA\CONTACT.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091", "SELECT Name_1 FROM tblContact_Companyslist WHERE (Com_ID like '%001%')")
Public Function GetTableForm(connectionString As String, _SQL As String) As System.Data.DataTable
Dim ds As New System.Data.DataSet()
Dim conn1 As New System.Data.OleDb.OleDbConnection(connectionString)
conn1.Open()
Dim adapter1 As New System.Data.OleDb.OleDbDataAdapter(_SQL, conn1)
adapter1.Fill(ds)
Return ds.Tables(0)
End Function
Private Sub edit_Click(sender As Object, e As EventArgs) Handles edit.Click
'check for the selected item in list
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim str1 As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim dt As System.Data.DataTable = GetTableForm("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\admim\Documents\ฐานข้อมูล5.accdb", "SELECT * FROM Tb_History WHERE (His_product)")
Me.semat.Text = str1
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = str1
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
'close connection
con.Close()
End If
End If
End If
End Sub
'check for the selected item in list
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As string = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' ", con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = intsi
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
End If
End If
End If
End Sub
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
intsi = intsi.Replace("'", "")
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' ", con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = intsi
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
'close connection
con.Close()
End If
End If
End If
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
ตอบคุณ Freedom
พอเอา Dim intsi As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value มันขึ้นว่า Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History WHERE His_product like '%101ส้ม%'", con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = intsi
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
'close connection
con.Close()
End If
End If
End If
Try
Dim cmd As New OleDbCommand
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
cmd.Connection = con
'check whether add new or update
If Me.semat.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = " INSERT INTO Tb_History(His_product, His_number, His_student, His_teacher, His_date, His_time)" & _
" VALUES ('" & Me.semat.Text & "'," & Me.txtnum.Text & ",'" & _
Me.txtna1.Text & "','" & Me.txtna2.Text & "','" & Me.txtdate.Text & "','" & _
Me.txttime.Text & "')"
cmd.ExecuteNonQuery()
Else
'update data in table
cmd.CommandText = "Update Tb_History SET " & _
" His_product= '" & Me.semat.Text & "' " & _
" ,His_number=" & Me.txtnum.Text & _
" ,His_student= '" & Me.txtna1.Text & "' " & _
" ,His_teacher= '" & Me.txtna2.Text & "' " & _
" ,His_date= '" & Me.txtdate.Text & "' " & _
" ,His_time= '" & Me.txttime.Text & "' " & _
" WHERE His_product= " & Me.semat.Tag
cmd.ExecuteNonQuery()
End If
'refresh data in list
RefreshData()
'close connection
con.Close()
MessageBox.Show("บันทึกลงตารางเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
และพอกดปุ่มแก้ไข มันerrorที่ Code (VB.NET)
Dim intsi As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
โดยขึ้นว่า Column named His_product cannot be found.
Parameter name: columnName
โค้ดปุ่มแก้ไข Code (VB.NET)
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' ", con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = intsi
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
End If
End If
End If
'check for the selected item in list
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As string = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim strSQL As String = "SELECT * FROM Tb_History " & _
" WHERE His_product = '" & intsi & "' "
Dim da As New OleDbDataAdapter(strSQL,con) '***** Debug เอาค่า SQL ออกมาดูหน่อยครับ (เอาเม้าส์มาคลิกที่บรรทัดนี้และกดปุ่ม F9 และ Run Program ตามปกติ)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = intsi
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
End If
End If
End If
End Sub