Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

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???

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.