diogoarsousa Posted April 18, 2018 at 12:45 PM Report #610266 Posted April 18, 2018 at 12:45 PM Boa tarde, Ao fazer um update de um registo em Access utilizando o VB.NET e o ADO.NET , obtenho este erro e não compreendo porquê. Está aqui o código do botão Update e o erro: Na linha: ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text Obtenho o erro: "A referência de objecto não foi definida como uma instância de um objecto." Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click Dim cb As New OleDb.OleDbCommandBuilder(da) ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text da.Update(ds, "AddressBook") MessageBox.Show("Data updated") End Sub Podem-me ajudar? Obrigado
M6 Posted April 20, 2018 at 08:38 AM Report #610293 Posted April 20, 2018 at 08:38 AM Isso deve vir com indicação da linha onde ocorre o erro. Só com essa informação, diria que o "da" refere não foi instanciado, ou seja, é uma variável que na verdade está a null nesse momento. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
diogoarsousa Posted April 20, 2018 at 10:39 AM Author Report #610298 Posted April 20, 2018 at 10:39 AM Mas no Form Load tenho: Private Sub Form_Ensaios_Primeira_Verificação_Contadores_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'SET THE DATABASE AND WHERE THE DATABASE IS TheDatabase = "/Ensaio_Contadores_2018.accdb" 'MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) MyDocumentsFolder = "C:\Ensaio Contadores 2018" FullDatabasePath = MyDocumentsFolder & TheDatabase '*********** 'SET UP THE PROVIDER dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Dados.accdb;Jet OLEDB:System database=system.mdw;" 'SET THE DATA SOURCE dbSource = "Data Source = " & FullDatabasePath 'SET THE CONNECTION STRING con.ConnectionString = dbProvider & dbSource '*********** 'OPEN THE DATABASE con.Open() 'STORE THE SQL STRING sql = "SELECT * FROM Tabela1" 'PASS THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER da = New OleDb.OleDbDataAdapter(sql, con) 'FILL THE DATASET WITH RECORDS FROM DATABASE TABLE 'Tabela = "Tipo_de_Caudal" da.Fill(ds, "AdressBook") 'CLOSE THE DATABASE con.Close() MaxRows = ds.Tables("AdressBook").Rows.Count inc = 0 txtFirstName.Text = ds.Tables("AdressBook").Rows(0).Item("First Name") txtSurname.Text = ds.Tables("AdressBook").Rows(0).Item("Surname") End Sub
diogoarsousa Posted April 20, 2018 at 03:04 PM Author Report #610300 Posted April 20, 2018 at 03:04 PM (edited) E em relação ao DataSet - ds: Dim ds As New DataSet 'HOLDS a DataSet OBJECT Public Class Form_Ensaios_Primeira_Verificação_Contadores Dim inc As Integer Dim MaxRows As Integer Dim con As New OleDb.OleDbConnection 'THE CONNECTION OBJECT Dim dbProvider As String 'HOLDS THE PROVIDER Dim dbSource As String 'HOLDS THE DATA SOURCE Dim MyDocumentsFolder As String 'HOLDS THE DOCUMENTS FOLDER Dim TheDatabase As String 'HOLDS THE DATABASE NAME Dim FullDatabasePath As String 'HOLDS THE DATABASE PATH Dim ds As New DataSet 'HOLDS a DataSet OBJECT Dim da As OleDb.OleDbDataAdapter 'HOLDS a DataAdapter OBJECT Dim sql As String 'HOLDS a SQL STRING Edited April 20, 2018 at 03:06 PM by diogoarsousa
M6 Posted April 20, 2018 at 03:35 PM Report #610301 Posted April 20, 2018 at 03:35 PM Coloca um breakpoint e faz debug passo-a-passo para veres qual a variável que devia estar instanciada e não está. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
diogoarsousa Posted April 24, 2018 at 03:14 PM Author Report #610341 Posted April 24, 2018 at 03:14 PM Tem de ser o ds - «O Dataset» Ele dá erro na linha: ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text Erro: A referência de objecto não foi definida como uma instância de um objecto. Como é que eu posso resolver isto? Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click Dim cb As New OleDb.OleDbCommandBuilder(da) ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text da.Update(ds, "AddressBook") MessageBox.Show("Data updated") End Sub
diogoarsousa Posted April 26, 2018 at 09:55 AM Author Report #610352 Posted April 26, 2018 at 09:55 AM (edited) Bom dia, A questão tem a ver com o Provider. O mesmo programa, em tudo igual, funciona perfeitamente com o PROVIDER=Microsoft.Jet.OLEDB.4.0 - com um ficheiro de uma versão do Access mais antiga - extensão MDB Mas dá o erro acima - com um ficheiro de uma versão do Access 2016 - extensão accdb Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Dados.accdb;Jet OLEDB:System database=system.mdw ---------- Já instalei Microsoft Access Driver (*.mdb, *.accdb) - This is the newer "ACE" ODBC driver Tirado do site da Microsoft: ' ''There are actually two (2) different Access ODBC drivers from Microsoft: ' ''Microsoft Access Driver (*.mdb) - This is the older 32-bit "Jet" ODBC driver. ' '' It is included as a standard part of a Windows install. ' '' It only works with .mdb (not .accdb) files. It is also officially deprecated. ' ''Microsoft Access Driver (*.mdb, *.accdb) - This is the newer "ACE" ODBC driver. ' '' It is not included with Windows, but it is normally included as part of a Microsoft Office install. ' '' It is also available as a free stand-alone "redistributable" installer for machines without Microsoft Office. ' '' There are separate 64-bit and 32-bit versions of the "ACE" Access Database Engine (and drivers), ' '' and normally one has either the 64-bit version or the 32-bit version installed. ' '' (It is possible to force both versions to exist on the same machine but it is not recommended as it can "break" Office installations. ' '' Therefore, if you already have Microsoft Office it is highly recommended that you use a Python environment that matches the "bitness" of the Office install.) Edited April 26, 2018 at 10:06 AM by diogoarsousa
diogoarsousa Posted April 27, 2018 at 01:04 PM Author Report #610358 Posted April 27, 2018 at 01:04 PM M6, Peço-lhe desculpa. Descobri o erro no programa. Uma coisa completamente estúpida: Escrevi "AdressBook" no preenchimento do DataSet: 'FILL THE DATASET WITH RECORDS FROM DATABASE TABLE da.Fill(ds, "AdressBook") E escrevia "AddressBook" com dois "dd" no código - o que, naturalmente, dava erro. ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text Obrigado pelo seu tempo. Diogo Sousa 1 Report
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now