The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Valnuke (9) [Avatar] Offline

I have a problem with a table Address. It has a FK field in it, CountryID (integer FK to the Country table). this field DOES NOT accept NULL values

I have no problem in visualizing the table, setting the combobox properly in the datagridview, but only until the new row line.

then, the datagrid starts giving me a lot of errors, probably because he can't retrieve the countryname from countryid because on creation the countryid is null.

but I haven't saved the item so why he wants to have a FK already? I need to have time to insert it

It's strange because if I set the countryID fk to accept NULL values then it gives no error, but I can't have a null value in that field so what should I do?

Do you know how can I solve this problem without having to set that column nullable?

jwooley (123) [Avatar] Offline
Re: datagridview with combobox column - Error with new row line
One option you can use is to set the default value in the OnCreated partial method implementation to supply a valid default value. Often with combo boxes, you can bind an empty string with the description of "Select". This requires that you insert the blank record into the underlying datasource of the combobox itself.

Alternatively, assuming you can set the nullvalue on the binding properly, you may want to consider changing the type on the entity class to nullable even if the underlying datavalue from the table is not nullable. You would then add a validation rule on the entity to state that the value is required before you submit to the database. Remember, often, your entities should not exactly mimic the physical representation on the database, so this seeming mismatch is not unexpected.

Valnuke (9) [Avatar] Offline
Re: datagridview with combobox column - Error with new row line
Thanks for the answer, these are the solutions I had tried also after a lot of trying

I didn't like the 1° very much because it generates some other errors, for example if you press "add new" by mistake the new row gets created and you have to manually delete and update the db

but the 2° option is great, the validation in the partial class can take care of the differences with the db...