Cesar Francisco

How to include in one Save event a Lookup event before commit a record to a database




Re: Visual Basic Express Edition How not to save duplicate record into a database?

Bruno Yu - MSFT

Cesar Francisco,

Please try the following code and add the Lookup event in the sub:

Code Snippet

Public Sub SaveChanges()

Dim table As DataTable = CType(datagridAuthors.DataSource, DataTable)

Dim changedRows As New ArrayList

For Each row As DataRow In table.Rows

If row.RowState <> DataRowState.Unchanged Then

changedRows.Add(row)

End If

Next

If changedRows.Count = 0 Then

Return

End If

Dim connection As New SqlConnection(ConnectionString)

connection.Open()

Dim adapter As New SqlDataAdapter(GetAllAuthorsSqlString, connection)

Dim builder As New SqlCommandBuilder(adapter)

Dim rows() As DataRow = CType(changedRows.ToArray(GetType(DataRow)), DataRow())

adapter.Update(rows)

adapter.Dispose()

connection.Close()

menuSaveChanges.Enabled = False

End Sub






Re: Visual Basic Express Edition How not to save duplicate record into a database?

Cesar Francisco

Thanks Bruno, good to see you again. Appreciate your help.

I would rather would go this way -(I include a rough sketch of it, here below) -,

because I do not use "connection" to the database ( am I wrong ):

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

'I was thinking to put a Query here

'then

--------

Dim result As Integer = 0

result = Table1BindingSource.Count

If result =0 Then

Me.Validate()

Table1BindingSource.EndEdit()

Dim rowsAffected As Integer = 0

rowsAffected = Table1TableAdapter.Update(AmDBDataSet.Table1)

MsgBox(rowsAffected.ToString + " Record Saved")

Else

Msgbox("Duplicate record")

or Dialogbox: "save it or not "

----------