Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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");
Posted

1 solution

Here in mob number you are set allownulls true. so if you are not giving any mobile number then it will pass as null from c# to db.so it will accept null value.if you want to set default value then set it into allow nulls is true or you can handle in C3 side by checking if values are null set to 0.
 
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