FormView tries to insert Null from Blank TextBoxes - entity-framework

I'm using a FormView control with the EntityFramework. I'm trying to add a new record, but every time I leave a text box blank, instead of inserting an empty string, it tries to insert a null.
How do I control this behavior so that it will use an empty string instead of a null?

you can handle this in the different levels of your application:
1) you can set default value in ItemCreated event handler and set default value there.
2) If you have permission to modify dblayer, you can set default value for column in db (or, maybe it is already set) and then set StoreGenerationPattern to Computed (look at, for example Entity Model - How to use Database's Default Column Values while inserting)

Related

Parse ClassFormDefinition for a Custom Page Type Field's Values

I want to populate a dropdown in my webpart from a custom page type field's values.
so for e.g I have a custom pagetype my.pagetype which has a field called myfield and has value like 1,2,3. How Do I get these 1,2,3 values ?
How do I access through SQL or macro so far I have reached till here and this code is giving me a long XML Schema string which has all fields definition and values of the custom page type.
CMSContext.Current.GlobalObjects.Classes["my.pagetype"].ClassFormDefinition
How do I get around?
Are you populating the drop down list in that Page Type manually? (e.g. 1, 2, 3)
If you want to reuse the same list of value in multiple places, it's better for you to create a custom table to store those value (e.g. customtable_myListX). Then for the drop down list, you can use the SQL option (SELECT Value, Display FROM customtable_myListX).

"The data has been changed" error when editing underlying record in Access VBA

I have a form in Access where I have 2 unbound multi-select listboxes, with some code to move items between them.
Each of the fields in the table which are shown in the listboxes are boolean values - if the value is true then the name of that field shows up in lstSelected, and if false shows up in lstUnselected.
The listboxes have a RowSourceType of Value List, and the value list is generated programatically by looking at the underlying record and constructing a string with the field names where the boolean values are true for lstSelected and False for lstUnselected.
On the form I have two buttons, cmdMoveToSelected and cmdMoveToUnselected. When I click on cmdMoveToSelected it changes the boolean value of the underlying field for any selected items in the lstUnselected listbox from false to true by executing an SQL string, then rebuilds the value lists for both of the listboxes.
I have all of this working just fine. If I do a me.lstUnwanted.requery and a me.lstwanted.requery then everything moves and shows up correctly, and the underlying fields are edited correctly, BUT when I click on anything else on the form I get the error:
The data has been changed.
Another user edited this record and saved the changes before you attempted to save your changes.
Re-edit the record.
Now I've found a way around this (jobDetailsID is the primary key of the record being dealt with):
Dim intCurID as Integer
intCurID = Me.JobDetailsID
Me.Form.Requery
Me.Recordset.FindFirst "JobDetailsID = " & curID
This requeries the form and then moves back to the current record, and this gets rid of the error, however it causes there to be a delay and the form to flicker while it opens back at the first record, changes back to the correct record and repopulates the list boxes.
Is there a way to do away with this error, or get it to trigger programmatically so I can catch it by turning the warnings off via vba?
Thanks in advance.
Maybe it helps not to bind the form to the table being altered by cmdMoveToSelected, but to a query that doesn't contain all the boolean fields. If cmdMoveToSelected alters one or more boolean fields, the record is changed, but the query result isn't. Not sure if it's sound though.
It also sounds a bit like a design problem rather than a form problem, storing options in boolean fields instead of into a related table.
Probably the best solution would be to not directly update the current record in the table while the Form is dirty. Instead, update the values of the fields within the form itself (Me!FieldName) as the items are moved from one List Box to the other, and let the form write those values back to the table as usual.
I seem to have fixed it, though the fix doesn't make a great deal of sense to me.
I added in a Me.Refresh to the button click code, after I had requeried the two listboxes and it appears to have stopped the message from coming up. However this only works when I have the JobDetailsID textbox visible on the form (though I expect this is arbitrary and any field-linked textbox would work).
Can anybody explain to me why this works? I'd like to understand fully when to use requery, refresh etc
I've had this sort of thing happen when I've left the form RowSource query hanging in place after converting the controls to unbound textboxes, etc. The general Form rowsource query (to bring in all fields I might possibly end up using) provides me with a query-list identical to the table fieldnames, making it simple to select them for control-names as needed. Works fine, but you have to remove the form rowsource query after all the names are matched-up. (After which DLookup and BeforeUpdate works for getting and storing values and changes.)

Date field default value must need to be Null in SugarCRM

I am using Sugar Professinal 6.4.4.
The problem i am facing is this, that when i create a custom field of type Date (Not DateTime) and don't assign a default value to it. Then when i use that custom field in Edit and Detail View of a module and save the record without assigning any value to that field in Edit View, Sugar after saving the record automatically assign the current date to that field which gets visible in Detail View. Though in database the value for that custom field is still Null.
How can i stop this defualt behaviour of automatically assigning current value to that custom field?
If you want to save the default value as NULL, you can set the default value in fieldset as
['display_default']= "''";
If you want to save the current date, use this
['display_default']='now';

Invisible value when creating new record

I have problem with some form in Access 2010. In one of the fields (Amount) I have as a value expression =IIf([IsNull([Code]);1;2) . In the form the values 1 and 2 are shown correctly but when I try to save the newly created record I have an error: You have to fill the Amount field. Can someone tell me why the value is not visible when saving new record to database?
You need to use VBA code on the NewRecord and/or BeforeUpdate event to insert these values into this field/textbox. What you are doing here amounts to setting a value for Display only.

How do I make large text boxes use null instead of blank string when empty

Here is my issue. I am currently forced to use Access and I am writing some generic validation that I can add to forms.
It was all going well and catching empty fields in form_error based on the error "You tried to assign the Null value to a variable that is not a Varient data type"
All of my required varchar fields are NOT NULL.
Unfortunately if a textbox has a control source to a large varchar DB field it behaves differently. I can't remember the size threshold but assume this behaviour difference would be equivalent to text vs. memo in an access table).
Basically, if you delete the contents of a small text box control it attempts to write null and the error is caught. All good.
If you do the same on a text box linked to a larger varchar, or memo database field then it writes a blank string which is considered valid.
I have confirmed this by changing the db Schema between varchar(50) and varchar(256), updating the linked table in Access and restarting Access for good measure.
I am hoping someone can point me to a property to set or some tiny piece of generic code that can be added to make all text boxes behave the same regarding writing NULL/Empty string when they are empty regardless of the size of the DB field they are connected to.
Just to note that also the box behaves differently on insert or edit. If not filled on insert it does leave the DB entry as null.
That's pretty much the way you have to do it. You could set up a "Validation Rule" on each text field, but again that would require hunting down all the text controls.
You can make that job easier. If you check the Object Dependencies of the tables, you can get a list of all the forms (and queries, etc.) involved. Then you can be sure you have hit each one.