Updating BO causes Business layer exception


Author
Message
StrataFrame Team
S
StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Hehe, sorry for the delay, been digging at the pile on my desk Smile  OK, that error you're getting is because the business object's CurrentDataTable thinks that it does not the DataColumn for the field(s) that are part of the PrimaryKeyFields array.  So, if the PrimaryKeyFields array contains "ID1" and "ID2", then the CurrentDataTable of the business object needs to have the columns for ID1 and ID2 (otherwise, the business object won't be able to send the primary key to the server to update the record, because the BO can't find the PK's column within its internal DataTable).  What I would do it put a break point on the call to your BO.Save(), and in the Watch window, check the visualizer for the BO.CurrentDataTable property (by clicking on the little magnifying glass, you'll get a window with a grid that shows the contents of the table).  Make sure that the table actually contains the column that is your primary key.
Doug Birtell
Doug Birtell
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 33, Visits: 64
Ben, I understand what you are saying.  Every where I look, I see the primary key field "SampleNumber" defined correctly, i.e. in the table, in the BO, etc.
I put a breakpoint in the BeforeSave() method of the business object and when I examine the primary key values, it is correct.  I will attempt to attach screenshots of various pieces of this "puzzlement".


SampleBO.designer.cs

        private static string[] _PrimaryKeyFields = new string[] { "SampleNumber" };
        /// <summary>
        /// Gets the field or fields that comprise the primary key for the business object.
        /// </summary>
        /// <remarks></remarks>
        public override string[] PrimaryKeyFields
        {
            get
            {
                return _PrimaryKeyFields;
            }
        }
       
       
 /// <summary>
 /// SampleNumber
 /// </summary>
 /// <remarks></remarks>
 [Browsable(false),
  BusinessFieldDisplayInEditor(),
  Description("SampleNumber"),
  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
 public System.String SampleNumber
 {
     get
     {
  object loValue;
  loValue = this.CurrentRow["SampleNumber"];
  if (loValue == DBNull.Value)
  {
      return String.Empty;
  }
  else
  {
      return (System.String)loValue;
  }
     }
     set
     {
  if (value != String.Empty)
  {
      this.CurrentRow["SampleNumber"] = value;
  }
  else
  {
      this.CurrentRow["SampleNumber"] = DBNull.Value;
  }
     }
 }


        /// <summary>
        /// A PropertyDescriptor class used to Get and Set the value of the SampleNumber property.
        /// </summary>
        /// <remarks></remarks>
        private class Field_SampleNumber_Descriptor : MicroFour.StrataFrame.Business.FieldPropertyDescriptor
        {
            public Field_SampleNumber_Descriptor() : base("SampleNumber") { }
            private System.Type _PropertyType = typeof(System.String);
            public override object GetValue(Object component)
            {
                return ((SampleBO)component).SampleNumber;
            }
            public override void SetValue(Object component, object Value)
            {
                ((SampleBO)component).SampleNumber = (System.String)Value;
            }
            public override System.Type PropertyType
            {
                get
                {
                    return this._PropertyType;
                }
            }
            public override System.Type ComponentType
            {
                get
                {
                    return _ComponentType;
                }
            }
        }
      

Attachments
SampleBOissue.doc (95 views, 50.00 KB)
StrataFrame Team
S
StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
When you're at that break point in the BeforeSave, check these values in the Watch window and let me know what you get:

boVariable["SampleNumber"]

and

boVariable.CurrentRow["SampleNumber"]

and

boVariable.CurrentDataTable.Columns["SampleNumber"]

Doug Birtell
Doug Birtell
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 33, Visits: 64
Ben,

I've attached a word document with screenshots that hopefully captures what you are wanting to see.

Most of what I see in the local varialbles of the BO looks ok.  The exception being the DataSet value which is null.  Hopefully this will make sense to you when you see the screenshots.

Thanks for all your help. I know it's something probably very simple that I've done, but I've not been able to find it.

Doug

Attachments
SampleBOissue2.doc (91 views, 69.00 KB)
StrataFrame Team
S
StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)StrataFrame Developer (4.6K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
What do you get in the Watch window when you test

boVariable.IsPrimaryKeyField("SampleNumber")

?  It should return true... if it's not, then that's the root of our problem.

Doug Birtell
Doug Birtell
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 33, Visits: 64
Ben,

I'm afraid I get true.  see attached...

Doug

Attachments
SampleBOissue3.doc (96 views, 29.00 KB)
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.5K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
The thing that pops out to me is that you are allowing NULLs in your PK (you have set to return an alternate value of an empty string if the db value is NULL). This may not be causing your problems here, but it has caused me problems in the past. First, while you can allows NULLs in a PK column, this is a bad idea. What will happen is that one record will be allowed a NULL value in the PK, then when a second record with a NULL PK added, it will fail. The record with a Null value can also get "lost" as most search criterion and joins on the PK won't include it. These can be tough bugs to hunt down (yep, I've done this Sad )



You need to setup the PK field in the database to be the PK (unique values, clustered likely), then setup the BO such that it doesn't allow nulls at all. If you are generating the PK automatically, you can set its value in the SetDefaults event (I think that is the event name...it's one of the two events that are in a BO when you create it, in the main file). If the user is entering the PK, then you will likely need to check that it is unique in the other event in the BO, CheckBusinessRules (again, I think that is what it's called).



I doubt this is the causing the current problem, but thought I'd point it out.
Doug Birtell
Doug Birtell
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 33, Visits: 64
I understand and agree totally about the PK not being null.  I had the BO setup that way originally, but when I do, I get the following compile error.  (see attached document)

The only way I could get it to work was to change the PK within the BO to "return alternate on null" with String.Empty.

I must be doing something terribly wrong....

Attachments
SampleBOissue4.doc (95 views, 32.00 KB)
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.5K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I wouldn't say terribly wrong...there's just lots of moving pieces and looks like you haven't figure out a couple of them Hehe



OK, I'm guessing that error is returned at runtime, not compile time (i.e. it's an exception being thrown, not a compile error). The problem is likely that you aren't creating your PK and setting the value before you save the BO (or something like that). In the Event Handlers region of your BO (main file), there is the SetDefaultValues handler. This is an excellent place to set the value of the PK, if it can be set by your app. E.g.



Private Sub SampleBO_SetDefaultValues()

'-- Set the PK value, in this case using a private method called GetNewSampleNumber (which would

' return a new unique sample number).

Me.SampleNumber = Me.GetNewSampleNumber()

End Sub




If the PK is set by the user, then you'll need to check that it is actually set. The easiest way is to just indicate that the field is required. Open the BO's designer, hit F4 to view the properties and look for the ReqiredFields property. You can easily set the PK field as required and they won't be able to save without it.



There is one more step you likely will want to take if the user is providing the PK: check that it is actually unique. There are a couple of ways to do this. The first is to check for a sql duplicate exception when saving...not the best way, but it can work. The better way (according a recent post about this very thing) is to check if it's unique using the ExecutScalar method. You'd do this in the CheckRulesOnCurrentRow event. E.g.



Private Sub MyBo_CheckRulesOnCurrentRow(ByVal e As Microfour.StrataFrame.Business.CheckRulesEventArgs)

'-- Check that the value provide is unique (if one was provided)

Dim sampleNumberExists As Boolean

Using cmd As New SqlCommand()

cmd.CommandText = "If Exists(Select SampleNumber" _

& " From SampleMaster" _

& " Where SampleNumber = @sampleNumber)" _

& " Begin Select 1 End" _

& " Else" _

& " Begin Select 0 End"

cmd.Parameters.Add("@sampleNumber",varchar,50)

cmd.Parameters("@sampleNumber").Value = Me.SampleNumber

sampleNumberExists = CBool(Me.ExecuteScalar(cmd))

End Using



'-- If the sample exists, add a broken rule

Me.AddBrokenRule(SampleBOFieldNames.SampleNumber, "The Sample Number indicated is already in use.")

End Sub




Note I just typed in the code, so there might be some syntax/typing errors. Hopefully this helps you understand how to manage the PK.



Of course, this might have nothing to do with the original problem.
Doug Birtell
Doug Birtell
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 33, Visits: 64
Greg,

I'm tracking 100% with what you posted.  The PK is entered by the user.  I have the PK in the BO set as "required" and "does not allow null".  I am using the strataframe maintenance form.  When I go to add a new record, the exception "Unable to cast object of type 'System.DBNull' to type 'System.String'." is thrown on the PK immediately when I click the "new" button on the form.  This is before I can add any data (PK) via the form.  The only way to get around this exception (that I've found) is to set the BO to "return alternate on null" with a String.Empty.  Doing this allows me to add new records via the form.  But... as previously discussed, this is not an ideal way to handle a PK value.

It acts as though it is adding the record to the BO before the user is allowed to enter any data via the form.

Now... when I have an existing record with a PK populated in the form (and the BO obviously), and I click "Edit" and then immediately click "Save" without ever changing any data, I get the following...

"BusinessLayerException
  An error occurred while saving an the data to the server.
DataLayerSavingException
  Cannot create UPDATE command because the updating DataTable does not contain columns for all PrimaryKeyFields.
DataLayerException
  Cannot create UPDATE command because the updating DataTable does not contain columns for all PrimaryKeyFields.

Source     : MicroFour StrataFrame Business

I get this regardless of how I have the PK setup.

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search