zephirus Posted May 5, 2009 at 07:11 PM Report #261670 Posted May 5, 2009 at 07:11 PM Olá, Tenho este código no meu programa: Imports System.Data.OleDb Friend Class frmCustomers Dim daCustomers As New OleDbDataAdapter() Dim dsCustomers As New DataSet() Public MachineID As String Public State As HowtoSaveRecord.gModule.FormState Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cnCustomers As OleDbConnection cnCustomers = New OleDbConnection Try With cnCustomers If .State = ConnectionState.Open Then .Close() .ConnectionString = cnString .Open() End With Catch ex As OleDbException MsgBox(ex.ToString) End Try If State = gModule.FormState.adStateAddMode Then Try Dim qryCustomers As String = "SELECT * FROM Machines WHERE MachineID = '" & MachineID & "'" daCustomers.SelectCommand = New OleDbCommand(qryCustomers, cnCustomers) Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(daCustomers) daCustomers.Fill(dsCustomers, "Machines") Catch ex As OleDbException MsgBox(ex.ToString) Finally cnCustomers.Close() End Try ElseIf State = gModule.FormState.adStateEditMode Then Dim qryCustomers As String = "SELECT * FROM Machines WHERE MachineID = '" & MachineID & "'" daCustomers.SelectCommand = New OleDbCommand(qryCustomers, cnCustomers) Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(daCustomers) daCustomers.Fill(dsCustomers, "Machines") Dim dt As DataTable = dsCustomers.Tables("Machines") Try txtMachineID.Text = dt.Rows(0)("MachineID") txtMachineName.Text = dt.Rows(0)("MachineName") txtMachineIP.Text = IIf(IsDBNull(dt.Rows(0)("MachineIP")), "", dt.Rows(0)("MachineIP")) txtMachineType.Text = IIf(IsDBNull(dt.Rows(0)("MachineType")), "", dt.Rows(0)("MachineType")) txtMachineOS.Text = IIf(IsDBNull(dt.Rows(0)("MachineOS")), "", dt.Rows(0)("MachineOS")) Catch ex As OleDbException MsgBox(ex.ToString) Finally cnCustomers.Close() End Try End If End Sub Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click Me.Close() End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim dt As DataTable = dsCustomers.Tables("Machines") If txtMachineID.Text = "" Or txtMachineName.Text = "" Then MsgBox("Please fill up Customer ID or Company Name information.", MsgBoxStyle.Critical) Exit Sub End If Try If State = gModule.FormState.adStateAddMode Then ' add a row Dim newRow As DataRow newRow = dt.NewRow() newRow("MachineID") = txtMachineID.Text dt.Rows.Add(newRow) End If With dt .Rows(0)("MachineID") = txtMachineID.Text .Rows(0)("MachineName") = txtMachineName.Text .Rows(0)("MachineIP") = IIf(txtMachineIP.Text = "", System.DBNull.Value, txtMachineIP.Text) .Rows(0)("MachineType") = IIf(txtMachineType.Text = "", System.DBNull.Value, txtMachineType.Text) .Rows(0)("MachineOS") = IIf(txtMachineOS.Text = "", System.DBNull.Value, txtMachineOS.Text) daCustomers.Update(dsCustomers, "Machines") MsgBox("Record successfully saved.", MsgBoxStyle.Information) End With Catch ex As OleDbException MsgBox(ex.ToString) End Try End Sub End Class Basicamente eu introduzo valores em 5 campos e faço save. Quando o faço na primeira vez, tudo corre bem. O problema é quando faço a seguir. Dá-me o seguinte erro na linha "daCustomers.Update(dsCustomers, "Machines")", perto do final do código. http://img22.imageshack.us/img22/9642/errorrnw.jpg Já percorri todo o código e não consigo perceber onde está o erro. Já procurei informação aqui no fórum e também não chego lá. Alguém me pode dizer onde está o problema? Obrigado.
vbtipo Posted May 5, 2009 at 08:35 PM Report #261716 Posted May 5, 2009 at 08:35 PM Experimenta em pores um break point, para veres qual é a situação? Lema: Se eu não saber de alguma coisa não se preocupem porque tento sempre ajudar alguma coisita, nem que seja, por palpites/sugestões.
zephirus Posted May 5, 2009 at 08:36 PM Author Report #261718 Posted May 5, 2009 at 08:36 PM Experimenta em pores um break point, para veres qual é a situação? Como assim? Para ver qual o erro? É que só me aparece essa mensagem, não dá mais erro nenhum.
nokPT Posted May 6, 2009 at 04:31 PM Report #261893 Posted May 6, 2009 at 04:31 PM Tens o Enf if antes do With dt, não será isso? Ricardo Timóteo
zephirus Posted May 6, 2009 at 04:45 PM Author Report #261902 Posted May 6, 2009 at 04:45 PM Tens o Enf if antes do With dt, não será isso? Tentei e aparentemente não é disso. O erro continua a acontecer na linha: daCustomers.Update(dsCustomers, "Customers") Não consigo mesmo entender o porquê. Terá a ver com o facto de a ligação à base de dados não estar aberta aquando da gravação???
zephirus Posted May 6, 2009 at 04:53 PM Author Report #261909 Posted May 6, 2009 at 04:53 PM Este código (e o resto dele) está disponível nesta página: http://www.sourcecodester.com/net/how-addupdatedelete-record-using-ms-access-database.html Talvez assim seja mais fácil ajudar. Já reportei o erro ao autor e estou à espera de feedback também.
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