简体   繁体   中英

error when update data ms access using visual basic 2010

**i have simple application, but i dont know how to fix it. this pic when i try to edit my database--> http://i861.photobucket.com/albums/ab171/gopak/sa_zps5a950df5.jpg

when i click button edit i want my access data will be update.this is my code.. thanks for your advice**

Imports System.Data.OleDb
Public Class Form2
Public cnstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gop's\Downloads\admin site\admin site\admin site\bin\Debug\data_ruangan.accdb"""
Public cn As New OleDbConnection
Public cmd As New OleDbCommand
Public adaptor As New OleDbDataAdapter

Private Sub logout_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles logout_btn.Click
    Form1.Show()
    Me.Close()

End Sub

Private Sub exit_btn_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exit_btn.Click
    Dim a As Integer
    a = MsgBox("Are you sure want to exit application?", vbInformation + vbYesNo, "Admin Site Virtual Tour Application")
    If a = vbYes Then
        End
    Else
        Me.Show()
    End If
End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'Data_ruanganDataSet.data_ruangan' table. You can move, or remove it, as needed.
    Me.Data_ruanganTableAdapter.Fill(Me.Data_ruanganDataSet.data_ruangan)

End Sub

Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim i = DataGridView1.CurrentRow.Index
    Label7.Text = DataGridView1.Item(0, i).Value
    txtName.Text = DataGridView1.Item(1, i).Value
    txtLocation.Text = DataGridView1.Item(2, i).Value
    txtCapacity.Text = (DataGridView1.Item(3, i).Value).ToString
    txtOperational.Text = (DataGridView1.Item(4, i).Value).ToString
    txtInformation.Text = DataGridView1.Item(5, i).Value
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'If txtName.Text <> "" And txtLocation.Text <> "" And txtCapacity.Text <> "" And txtOperational.Text <> "" And txtInformation.Text <> "" Then
    Dim i = DataGridView1.CurrentRow.Index
    Dim ID = DataGridView1.Item(0, i).Value
    Dim cmd As New OleDb.OleDbCommand

    If Not cn.State = ConnectionState.Open Then
        cn.Open()
    End If

    cmd.Connection = cn
    cmd.CommandText = ("update data_ruangan set Name = '" & txtName.Text & _
        "',Location = '" & txtLocation.Text & "',Capacity = '" & txtCapacity.Text & _
        "',Operational_Hours ='" & txtOperational.Text & "',Information = '" & txtInformation.Text & ";")
    cmd.ExecuteNonQuery()
    cn.Close()
    txtName.Text = ""
    txtLocation.Text = ""
    txtCapacity.Text = ""
    txtOperational.Text = ""
    txtInformation.Text = ""
    'End If
End Sub

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

  End Sub
End Class

Try to use a parameterized query to execute your code. The error message is relative to the fact that you haven't initialized your connection with the information contained in the ConnectionString.
Here an example on how to do that...... but......

Dim cmdText = "update data_ruangan set [Name] = ?,Location = ?,Capacity = ?, " & _
              "Operational_Hours =?,Information = ?;"
Using cn = new OleDbConnection( cnstring )
Using cmd = OleDb.OleDbCommand(cmdText, cn)
    cn.Open
    cmd.Parameters.AddWithValue("@p1", txtName.Text)
    cmd.Parameters.AddWithValue("@p2", txtLocation.Text)
    cmd.Parameters.AddWithValue("@p3", txtCapacity.Text)
    cmd.Parameters.AddWithValue("@p4", txtOperational.Text)
    cmd.Parameters.AddWithValue("@p5", txtInformation.Text)

    '''' WARNING ''''
    ' WITHOUT A WHERE STATEMENT YOUR QUERY WILL UPDATE 
    ' THE WHOLE TABLE WITH THE SAME VALUES
    '''' WARNING ''''

    cmd.ExecuteNonQuery()
End Using
End Using
txtName.Text = ""
txtLocation.Text = ""
txtCapacity.Text = ""
txtOperational.Text = ""
txtInformation.Text = ""

This code updates all the records of your table with the same value, so, unless you have only one record and update Always the same record you need to add a WHERE condition to your command.

Also, the NAME word is a reserved keyword in Access 2007/2010 and it is better to encapsulate that word with square brackets to avoid a syntax error message.

I have also removed the global variable OleDbConnection and used a local one that will be closed and destroyed when the code exits from the Using statement. This is the correct way to handle disposable objects, in particular every connection object is Always to be used in this way to release as soon as possible the expensive unmanaged resource used by the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM