The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Hi, I have a problem and I can't seem to solve it

Using scope As New Transactions.TransactionScope
Try
db.Jobs.DeleteOnSubmit(jobToDelete)
db.Shipments.DeleteOnSubmit(shipmentToDelete)
db.SubmitChanges
scope.complete
Catch ex As Exception
Msgbox(ex.tostring)
End Try
End Using

Let's say that I receive an error and I can't delete the shipmentToDelete from database. (maybe because its ID is used as FK in another table)

Even if I solve the problem, my datacontext still has these 2 object in the GetChangeSet.Deletes cache!!
so every time i try to do db.submit, for whatsoever reason, I get the same error

how to avoid it?

I would like to be able to clear somehow the db pending object cache in case of errors, otherwise I would have to create a new datacontext object, but I'm worried this could give problems the rest of my Windows Form...

thanks
Hello, I would like to implement something to log the updates in my database (to check which user updated what)

Since I'm quite a newbie programmer, I thought that you coul give me some good advice or point evident mistake in my code.

(I've also taken parts of code around on the internet(so thanks to these guys) but most of the examples were more complicated and written in c#)

I found 2 major issues on the internet:
1. none of the code I've seen around was dealing with the problem of avoiding logging if the item was not correctly updated... thing that I tried to solve by first adding the log information in a separate list
2. most of them were dealing with definied types of objects (customer, product) while what I want is a method that can deal with all of them


thanks


Partial Class myDataContext

Sub SubmitLogChanges()
'List of AuditLog objects, taken from the AuditLogs table in the database,
'a table with information about
'Username, date, table_name, modified item type, oldvalue, newvalue and ID

Dim AuditLogList As New List(Of AuditLog)

'For each update ready to be submitted
For Each item In GetChangeSet.Updates

'Get the table, object type, primary key
Dim myUpdatedTable = GetTable(item.GetType)
Dim pkeyfield = Mapping.GetTable(item.GetType).RowType.DataMembers.Where(Function(x) x.IsPrimaryKey)

Dim mmi = myUpdatedTable.GetModifiedMembers(item)
For Each mi In mmi

Dim newAuditRecord As New AuditLog
With newAuditRecord
'Get information about the logged user, date and values
.Username = myUserID
.Date = Now
.DbTable = mi.Member.Name
.Type = mi.Member.ReflectedType.Name ' same as item.GetType.Name
.OldValue = mi.OriginalValue
.NewValue = mi.CurrentValue
End With



'This is the only way I found (for now) to identify the object, looking for its
'primary key value....
Dim properties() = mi.Member.ReflectedType.GetProperties
If pkeyfield.Count = 1 Then
For Each pr In properties
If pr.Name = pkeyfield.FirstOrDefault.Name Then
With newAuditRecord
.ID = pr.GetValue(item, Nothing)
End With
End If
Next
Else
'If there are more than 1 PK, for example a combined PK....
If pkeyfield.Count > 1 Then
'to do, suggestion appreciated
End If
End If

'This add the Log object to a list
AuditLogList.Add(newAuditRecord)
Next
Next

Try
'Modify Database
SubmitChanges()

'insert audit values, if no errors showed up
AuditLogs.InsertAllOnSubmit(AuditLogList)
'Save also the changes in the AuditLogs Table
SubmitChanges()

Catch ex As Exception
Finally
AuditLogList.Clear()
End Try
End Sub
End class


If I get to have this correctly working, I'll work on a delete-logger method in the future
Ok, that's fine for me, I just wanted to know if it was the right way to do it...

thanks
Hello,

I have created an OnLoad partial method in the partial class definition. It sets some

It's something like this

Sub On load()
Me.SomePartialProperty = GetUpdatedData()
End Sub

This works fine, when the table loads I can see the updated data that I need in the column bound to the SomePartialProperty property

I would Like to know if there's a command to send that OnLoad method again, for example to display the updated data after some changes. something like:

me.bindingsource.reloadcurrent

thanks in advance

bye



ps. For now I've solved doing something like this, but it's not the same thing...
CType(me.bindingsource.current, object).SomePartialProperty = GetUpdatedData()
Hello, last time I asked I was told that it was ok to use the partial classes in an enviroment where you want to read the value of a certain item contained in another table accessing it thru the FK associations, like adding a new column in a customer DGV and bind it to a property like:

Public ReadOnly Property PartDeliveryCode() As String
Get
Return Me.Address.PostCode
End Get

this is shown in a grid, I have my customer table with list of customers and plus I show the postcode taken from the address table thru the addressFK in my customer table

but I have a serie of problems now... especially reguarding updating. When I try to update the address FK in the custome table, I can't do it if it's loaded in the table.

If I set the column postcode to invisible, I can update the FK in the customer table, but when the column is visible I can not, I guess because the object is currently used, thru the partial property.

So how can I solve this problem? How can I see the information about the postcode (in the grdi) but still be able to update the record?

thanks
I think I solved, I'm not sure how

I think that I was trying to change an object value while that was loaded somewhere else in the code.
Hello... I'm getting mad with this exception, I don't understand what I am doing wrong

I'm trying to update a record that has a FK field.

If I update the record when the FK is NULL, then everything goes fine, but if a FK is selected and I simply want to change it it gives me this exception.
ForeignKeyReferenceAlreadyHasValueException

I don't understand, why is it a problem to update this record? can you help me or you need more details? thanks

bye
Thanks for the answer, these are the solutions I had tried also after a lot of trying

I didn't like the 1° very much because it generates some other errors, for example if you press "add new" by mistake the new row gets created and you have to manually delete and update the db

but the 2° option is great, the validation in the partial class can take care of the differences with the db...
Hello,

I have a problem with a table Address. It has a FK field in it, CountryID (integer FK to the Country table). this field DOES NOT accept NULL values

I have no problem in visualizing the table, setting the combobox properly in the datagridview, but only until the new row line.

then, the datagrid starts giving me a lot of errors, probably because he can't retrieve the countryname from countryid because on creation the countryid is null.

but I haven't saved the item so why he wants to have a FK already? I need to have time to insert it

It's strange because if I set the countryID fk to accept NULL values then it gives no error, but I can't have a null value in that field so what should I do?


Do you know how can I solve this problem without having to set that column nullable?

thanks