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


Author
Message
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