Click here to Skip to main content
13,006,530 members (59,764 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Is null is acceptable for the number Column having Default value '0'?Can any one explain clearly?
Posted 21-Jan-13 2:21am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Having a field nullable has nothing to do whit it's type or default value. It is a separate property of the field. So you either make it nullable or not-nullable - thus it will accept accept or reject null values, independent of type and default value.
Having the default value set, means that it will use that value if the field is omitted on insert, but not if there is a null. See following example.
Let's suppose you have a table T with two numeric fields: D1 nullable-default 0, D2 not nullable. Now if you issue following statements:
insert into T(D1, D2) values(100,200) you will get 100,200
insert into T(D1, D2) values(null,100) you will get null,100
insert into T(D2) values(200) you will get 0,200 and there you see the default value.
hemantwithu 23-Jan-13 0:12am
But Here I am not inserting the record with direct insert query as "insert into table values(@c1,@c2)".I am inserting record using DataSet as like this
SqlConnection con = new SqlConnection(connStr);
DataSet ds = new DataSet();
DataRow dr;
SqlCommand cmd = new SqlCommand("select * from address",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cmdBuilder;
da.FillSchema (ds,SchemaType.Source );
da.Fill(ds, "address");

dr = ds.Tables["address"].NewRow();
dr["sname"] = "tomy";
dr["fname"] = "peter";


cmdBuilder = new SqlCommandBuilder(da);
da.Update(ds, "address");
Zoltán Zörgő 23-Jan-13 3:04am
The provider is working a little bit different: it won't take such properties from the schema! You use a select statement as source. The FillSchema won't have idea how to get the metadata from the original table.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Null and 0 are two different values.
Null indicates nothing was present and 0 indicates a value.

Thus, IMO, null and 0 are not the same and one cannot be replaced with the other.

Have a look at nullable types[^] for an integer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100