3 Replies - 1495 Views - Last Post: 17 June 2013 - 10:50 AM Rate Topic: -----

#1 allik7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 10

Constraint Exception

Posted 17 June 2013 - 09:11 AM

Hello All,

I am relatively new to C# programming and seek your assistance in solving this constraint exception error.

I and using a bindingnavigatortoolbar each time a click the add button i get this error "This property cannot be set to a null value " and this occurs at the line " _OtherPhone = StructuralObject.SetValidValue(value, false); "

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        public global::System.String OtherPhone
                return _OtherPhone;
                _OtherPhone = StructuralObject.SetValidValue(value, false);
        private global::System.String _OtherPhone;
        partial void OnOtherPhoneChanging(global::System.String value);
        partial void OnOtherPhoneChanged();

N.B. Entity Framework is been used to connect to the database. (windows application) The error does not happen on all fields. The database table is setup not to accept null values

Can someone say how i correct this problem. Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Constraint Exception

#2 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: Constraint Exception

Posted 17 June 2013 - 10:35 AM

It seems that your OtherPhone column in the database is non-nullable. So the property can't be set to null.
Open SQL Management Studio, connect to the database , navigate to the table containing the column and set it to be nullable.
You should remove the table from the model and add it again so it can catch the change.

PS : Or, if you want to go the other way around, you could catch the exception and print a message on the UI saying that the user must type the other phone before submitting the info. Personally, I think a column named OtherPhone should be optional.

This post has been edited by coder3788: 17 June 2013 - 10:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,438
  • Joined: 02-June 10

Re: Constraint Exception

Posted 17 June 2013 - 10:37 AM

Line 18 you define _OtherPhone as a string. Strings can't be null in this context of the database.

Line 13. The rsult of your computation/processing is resulting in null which you then try to send to _OtherPhone. That's not legal as a string can't be set to null in this context of the database.

So you need to check the results of your process (the right side of the equal sign in line 13). Either the process doesn't work like you thought. Or you programmatically will have to look for it and if the result is null then set _OtherPhone to string.empty.

This post has been edited by tlhIn`toq: 17 June 2013 - 11:48 AM

Was This Post Helpful? 1
  • +
  • -

#4 [email protected]  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Constraint Exception

Posted 17 June 2013 - 10:50 AM


Strings can't be null.

Strings can ordinarily be null, as they are a reference type. It's the IsNullable=false part in the EdmScalarPropertyAttribute, and the corresponding null constraint in the database that means the OtherPhone property and the mapped database field cannot be assigned null.

This post has been edited by [email protected]: 17 June 2013 - 11:03 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1