Home All Groups Group Topic Archive Search About

DefaultValue vs. NoNullAllowedException

Author
15 Jan 2007 2:40 PM
Bragadiru
Hi,

I defined in my Sql Server db a table with 2 int columns that don't allow
null values + their default values are set to 0.

I'm using a DataAdapter to read data, but it doesn't bring the default
setting :

using (SqlConnection conn = new SqlConnection("MyConnectionString"))

{

    SqlDataAdapter adapter = new SqlDataAdapter("Select * from MyTable",
conn);

    DataSet result = new DataSet();

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    conn.Open();

    adapter.Fill(result);

    conn.Close();

    if (null != result && result.Tables.Count > 0)

    {

        DataRow dr = result.Tables[0].NewRow();

        dr["Column1"] = 11;

        result.Tables[0].Rows.Add(dr); // <= NoNullAllowedException for
Column2, even it has a DefaultValue defined

    }

}



I don't want to set manually in the code the AllowDbNull or DefaultValue
properties.

What am I doing wrong ?

I can this sql statement without pbs :

INSERT INTO [Test] ([Column1]) VALUES (11)

Thanks for any advice

AddThis Social Bookmark Button