I have faced a problem with the dataset when inserting record into DB.I have a table address in which phno (AllowNulls=False,Defaultvalue=’0′) and mbno(AllowNulls=True,Defaultvalue=’0′) .While inserting record into database through dataset when I didn’t supply any input to the both columns phno is taking default value(‘0’) and mbno is taking “Null”.I suppose both has to take default value when there is no input.
I dont know why this is happening.I have written code 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");
In the remarks-section of this msdn reference page it is explained, what value will be inserted in which cases.
Since
AllowNulls = trueon your DataSet it probably submits anullto the database, which will let the database insert anull.If you want to have your default value inserted, remove
AllowNullsor explicitly remove the parameter from the insert statement (you would have to write your own InsertCommand in this case).Btw, what do you want archieve by the combination of AllowNull and Default?