3

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?

5
  • You have a table faxno which allows nulls? Do you mean a column instead? Apart from that, you show us more(database schema, code). Commented Jan 17, 2013 at 9:32
  • How did you create the row to add? There's a big difference between null and DBNull.Value when using a data-set... Commented Jan 17, 2013 at 9:34
  • If you execute the statement INSERT (Column) VALUES (NULL) (as an example), then NULL will be inserted. You only get the default if the column is omitted from the column list or you specify DEFAULT instead of NULL. I suspect that (underneath the covers), you're getting an INSERT which includes the column. Commented Jan 17, 2013 at 9:41
  • @Damien_The_Unbeliever I am inserting through dataset not insert query. Commented Jan 17, 2013 at 9:43
  • 2
    @JaiGanesh - at some point, at some level, an INSERT query is being generated. It may not be visible in your code, but it's happening. Commented Jan 17, 2013 at 9:46

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

If i exclude the faxno column the null is inserted instead of default value.
1

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.

8 Comments

I am inserting data through dataset not with straight insert query.
@Jai DataTable is part of the the DataSet API. I have not shown a "straight insert query".
If the column in question does not allow nulls, the DataTable will throw an exception (whether I try to add null or DBNull.Value).
@KonradMorawski if you have a default value defined in the DataTable (not the db table), then it should assume the default value.
But why wouldn't the DataTable "know" what the default value is? Surely the data adapter (that retrieves it from the actual database file - SqlCeDataAdapter in my case) would tell it?
|
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.