Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is null is acceptable for the number Column having Default value '0'?Can any one explain clearly?
Posted

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.
 
Share this answer
 
v2
Comments
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";

ds.Tables["address"].Rows.Add(dr);

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.
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900