Default values in a database?

Yuliya

Active member
Joined
Nov 9, 2007
Messages
34
Programming Experience
Beginner
I am working on a Windows form application where a user enters data which is saved in a database. There are several fields in a DataGridView that the user needs to fill out. I need to set a default value for some of the fields, so if the user doesn't specify the value, it would be set to the default.

So when I specified a table in the database, I put a default value in some of the columns (through Visual Studio db editor). I also set up those columns to not allow null values. But when I run my program, if I don't enter a value in those columns, I get an exception that null is not allowed, instead of setting it to the default value.

What am I doing wrong?

Thank you very much!
 
When a new DataRow is created EVERY field is NULL to begin with, then any auto-generated values or default values are set. Have a look at the properties of your DataColumn(s). Is there a value set for the DefaultValue property? If not then that is your issue.
 
I did a simple test where I set default value in database table/column definition, but the app didn't catch it, haven't had time to look more into this approach. But when I set default value in the dataset schema it worked. (edit Dataset in Designer, select column, set DefaultValue property) Without doing anything else the default value displayed in UI, and without touching the field that value was saved to db. This was tested with same result for both UI modes; Details/DataGridView.
 
Back
Top