Varchar Primary Keys, System.DBNull errors, and all that


Author
Message
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
No problem Smile
Chris Crabtree
Chris Crabtree
StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)
Group: Forum Members
Posts: 57, Visits: 107
Your other reply set me straight. Replace the two steps above starting with "Add a new method..." with the following single step:

  • In the BO designer, set the PrimaryKeyIsAutoIncremented property to False.

Much easier! Thanks, Trent!


Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
Chris,

As I mentioned on your other post, if you are going to be using VarChar primary keys I would create a subclassed BusinessLayer that already has the PrimaryKeyIsAutoincremented properyt set to false.  All you need to do is create a class that looks something like this:

class MyCustomBusinessLayer : MicroFour.StrataFrame.Business.BusinessLayer
{

     public
MyCustomBusinessLayer()
     {
         this.InitializeComponent();
     }

     private void InitializeComponent()
     {
         this.PrimaryKeyIsAutoIncremented = false;
     }
}

Once you have a custom business layer class created, you would then change any new business objects inherited class:

public partial class BusinessObject1 : MyCustomNamespace.MyCustomBusinessLayer

This way you do not have to recreate that code you had posted every time and it will respect the PrimaryKeyIsAutoIncremented property through the inherited class.


Chris Crabtree
Chris Crabtree
StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)StrataFrame User (157 reputation)
Group: Forum Members
Posts: 57, Visits: 107
As I am still new to the framework, I am making myself cheat sheets on how to do various things once I've figured it out the first time.

I wanted to post this here for two reasons: 1) in case someone might find it useful, and 2) to see if I am missing something, or, worse, doing extra work I don't have to be doing. Smile

So recently I defined a varchar field as PK for a table and spent a little while staring at the 'cannot convert System.DBNull to the Text property of...' error trying to figure out what to do. This is what I came up with.

Note that the last three bullet points are the only additional work on top of what you would normally do. It's just very confusing at first knowing what to change and where.

  • Design table as normal in DDT, with the varchar primary key
    • Don't worry about specifying a default value for the PK here
    • The DDT default value only applies at the database level, which is not where the problems arise
  • Create the BO in your project
  • Map the BO in the mapper as you normally would
  • Add the data retrieval methods to the BO as you normally would (FillAll(), etc.)
  • Add a new method where you turn PK auto-incrementing off

private void ConstructorCommon()

{

this.PrimaryKeyIsAutoIncremented = false;

}

  • Call this new method from each constructor
  • In the BO's SetDefaultValues method, give the PK a default non-null value.

private void WizardInstructionsBO_SetDefaultValues()

{

this.PageDescription = "DefaultPageDescriptionKey";

}

That's it! I used this BO on a maintenance form and everything is working as it should.

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