简体   繁体   中英

Why default value is not inserting in column when allow null is true?

I have a table address in which a column called "faxno" which allows null and has default value as 0.When we insert a record programmatically using dataset we are getting the faxno value as "Null" but not as "0".

I didn't understand why this is happening?

Is there any possiblity to insert default value instead of Null?

In your insert statement, exclude the column faxno .

In the case of a nullable column, the default value will be used when an insert occurs and the column name is excluded from the statement. To insert a null value, the column must be included in the list and the value of null must be inserted explicitly.

Reference

When adding a row to a DataTable , it depends on how you specify the values. If you specify null , it interprets it as "apply defaults". If you specify DBNull.Value , it interprets it as "this is explicitly null". So: pass null :

DataTable dt = new DataTable();
dt.Columns.Add("foo", typeof(int)).DefaultValue = 0;
Console.WriteLine(dt.Rows.Add(new object[]{null})["foo"]);
Console.WriteLine(dt.Rows.Add(new object[]{DBNull.Value})["foo"]);

The first line shows "0" - it has applied the default. The second line is empty - it is explicitly null .

The default value is inserted if and only if a value is not specified explicitly.

If I have a table Articles (ID int not null, Title nvarchar(50) null) with a default value of "Untitled" for the Title column, and I do:

Insert into Articles (ID) values (1)

it will insert the row (1, 'Untitled') . But if I do

Insert into Articles (ID, Title) values (2, null)

it will insert the row (2, null) because I explicitly requested that a null value (which is valid according to the column definition) be inserted in the Title column.

When you use a DataSet with a DataTable, if has to fill all the fields of the row with something, regardless of the default settings, and then saves all the fields when the DataTable is saved. If a value of the field has a value of the C# null object ( null ) than the DataSet treats that field as uninitialized, and does not set that field in the generated insert/update statements (that means that your default will be applied).

However, if the value of the field is Convert.DBNull (the SQL null value) than an explicit null value is sent to the database and it's inserted as such in the database, ignoring the defaults.

If you do it with a dataset, you probably did "select * from table where...." and then probably use the default GetInsertCommand statement when inserting, and in this case, it selects every field. If you did not submit any value to that field in the datarow, it will then be saved as NULL by the insert command instead of using the default value. (you can check the Command.CommandText property to know the SQL command that will be executed after the GetInsertCommand, and you should see the field there).

One option is that you define your own Insert command without that field. To do so, when you select to create your new datarow, you should select every field but the one with the default value and when the GetInsertCommand is executed, it will not be included and it will then insert the default value instead of NULL.

if you need the field to sometimes inserted anyway (because sometimes you may need to insert the value), you may need to create custom statements because sometimes you want the field, sometimes not to use the default. you can do so in creating a custom select line with a logic that for each column in the datarow, if in the column there is data then add it, else don't and you'll end up having a statement with only columns that have values and every other will use the table default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM