Hi Bruce,
Me too, it seems like the child row is locked or something.
Yes, I'm doing the select on the parent's insert command. I'm using
@@Identity at this point but plan on changing it to SCOPE_IDENTITY() once I
get the basic functionality working. I also have cascade set on for the
relationship (delete, Update, and Accept/Reject) on the typed dataset - but
not in the database.
Here is the .update code I am using:
If Not inDS.GetChanges(DataRowState.Added) Is Nothing Then
'don't allow customer additions here, reject any that come
in If Not inDS.Customer.GetChanges(DataRowState.Added) Is
Nothing Then
inDS.Customer.GetChanges(DataRowState.Added).RejectChanges()
End If
Me.sdaCustomerPrescription.Update(inDS.GetChanges(DataRowState.Added),
"Prescription")
Me.sdaCustomerPrescriptionDtl.Update(inDS.GetChanges(DataRowState.Added),
"PrescriptionDetail")
End If
I've pasted my select command and insert command code below which is based
on the results of the Dataadapter Configuration Wizard.
Note that my lowest level table, PrescriptionDetail has a two column Primary
Key: PrescriptionFK and PrescriptionDetailType. I can change that to have
an identity column as the primary key and then set a uniqueness constraint on
the two columns if you think that is source of the problem.
Thanks,
Carl
Code:
For Customer:
With CustomerSDA.SelectCommand
.CommandText = "SELECT CustomerKey, LeftPD, RightPD,
TotalNearPD, TotalDistPD FROM Customer WHERE" & _
" (CustomerKey = @InputCustomerKey)"
.Parameters.Add("@InputCustomerKey", System.Data.SqlDbType.Int,
4).Value = strCustomerKey
.Connection = SqlConnection1
End With
For Prescription:
With PrescritptionSDA.SelectCommand
.CommandText = _
"SELECT PrescriptionKey, CustomerFK, IssuedDate,
ExpirationDate, PrescriberFK, Special_Instr " & _
"FROM Prescription " & _
"WHERE (CustomerFK = @InputCustomerKey)"
.Parameters.Add("@InputCustomerKey", System.Data.SqlDbType.Int,
4).Value = strCustomerKey
.Connection = SqlConnection1
End With
'
'SqlInsertCommand1
'
With PrescritptionSDA.InsertCommand
.CommandText = "INSERT INTO Prescription(CustomerFK, IssuedDate,
ExpirationDate, PrescriberFK, Sp" & _
"ecial_Instr) VALUES (@CustomerFK, @IssuedDate, @ExpirationDate,
@PrescriberFK, @" & _
"Special_Instr); SELECT PrescriptionKey, CustomerFK, IssuedDate,
ExpirationDate, " & _
"PrescriberFK, Special_Instr FROM Prescription WHERE
(PrescriptionKey = @@IDENTITY)"
.Connection = SqlConnection1
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@CustomerFK", System.Data.SqlDbType.Int,
4, "CustomerFK"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@IssuedDate",
System.Data.SqlDbType.DateTime, 4, "IssuedDate"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@ExpirationDate",
System.Data.SqlDbType.DateTime, 4, "ExpirationDate"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@PrescriberFK",
System.Data.SqlDbType.Int, 4, "PrescriberFK"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Special_Instr",
System.Data.SqlDbType.NVarChar, 250, "Special_Instr"))
End With
For PrescriptionDetail (Note that the PK here is the PrescriptionFK plus the
PrescriptionDetailType):
With PrescriptionDetailSDA.SelectCommand
.CommandText = "SELECT PrescriptionFK, PrescriptionDetailType,
RDSpherical, RNSpherical, " & _
"RCylindrical, RAxis, RPrism1, RBase1, RPrism2, RBase2,
LDSpherical, LNSpherical, " & _
"LCylindrical, LAxis, LPrism1, LBase1, LPrism2, LBase2 " & _
"FROM PrescriptionDetail " & _
"WHERE (PrescriptionFK IN " & _
"(SELECT PrescriptionKey FROM Prescription WHERE
CustomerFK = @InputCustomerKey))"
.Parameters.Add("@InputCustomerKey", System.Data.SqlDbType.Int,
4).Value = strCustomerKey
.Connection = SqlConnection1
End With
'
'SqlInsertCommand1
'
With PrescriptionDetailSDA.InsertCommand
.CommandText = "INSERT INTO PrescriptionDetail(PrescriptionFK,
PrescriptionDetailType, RDSpherica" & _
"l, RNSpherical, RCylindrical, RAxis, RPrism1, RBase1, RPrism2,
RBase2, LDSpheric" & _
"al, LNSpherical, LCylindrical, LAxis, LPrism1, LBase1, LPrism2,
LBase2) VALUES (" & _
"@PrescriptionFK, @PrescriptionDetailType, @RDSpherical,
@RNSpherical, @RCylindri" & _
"cal, @RAxis, @RPrism1, @RBase1, @RPrism2, @RBase2,
@LDSpherical, @LNSpherical, @" & _
"LCylindrical, @LAxis, @LPrism1, @LBase1, @LPrism2, @LBase2);
SELECT Prescription" & _
"FK, PrescriptionDetailType, RDSpherical, RNSpherical,
RCylindrical, RAxis, RPris" & _
"m1, RBase1, RPrism2, RBase2, LDSpherical, LNSpherical,
LCylindrical, LAxis, LPri" & _
"sm1, LBase1, LPrism2, LBase2 FROM PrescriptionDetail WHERE
(PrescriptionDetailTy" & _
"pe = @PrescriptionDetailType) AND (PrescriptionFK =
@PrescriptionFK)"
.Connection = SqlConnection1
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@PrescriptionFK",
System.Data.SqlDbType.Int, 4, "PrescriptionFK"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@PrescriptionDetailType",
System.Data.SqlDbType.NVarChar, 15, "PrescriptionDetailType"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RDSpherical",
System.Data.SqlDbType.NVarChar, 10, "RDSpherical"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RNSpherical",
System.Data.SqlDbType.NVarChar, 10, "RNSpherical"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RCylindrical",
System.Data.SqlDbType.NVarChar, 10, "RCylindrical"))
.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RAxis",
System.Data.SqlDbType.NVarChar, 10, "RAxis"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RPrism1",
System.Data.SqlDbType.NVarChar, 2, "RPrism1"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RBase1", System.Data.SqlDbType.NVarChar,
2, "RBase1"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RPrism2",
System.Data.SqlDbType.NVarChar, 2, "RPrism2"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RBase2", System.Data.SqlDbType.NVarChar,
2, "RBase2"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LDSpherical",
System.Data.SqlDbType.NVarChar, 10, "LDSpherical"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LNSpherical",
System.Data.SqlDbType.NVarChar, 10, "LNSpherical"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LCylindrical",
System.Data.SqlDbType.NVarChar, 10, "LCylindrical"))
.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LAxis",
System.Data.SqlDbType.NVarChar, 10, "LAxis"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LPrism1",
System.Data.SqlDbType.NVarChar, 2, "LPrism1"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LBase1", System.Data.SqlDbType.NVarChar,
2, "LBase1"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LPrism2",
System.Data.SqlDbType.NVarChar, 2, "LPrism2"))
.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LBase2", System.Data.SqlDbType.NVarChar,
2, "LBase2"))
End With
Bruce Johnson said:
That surprises me, since the update to the child FKs should be done
through the cascade when the parent PK is updated.
Just to make sure, the reselect is done as part of the InsertCommand on
the Parent's DataAdapter, right?
INSERT INTO Parent (F1, F2) VALUES (@F1, @F2);
SELECT Id, F1, F2 FROM Parent WHERE (Id =
SCOPE_IDENTITY())
Bruce Johnson [.NET MVP]
http://www.objectsharp.com/blogs/bruce