ASP.NET/C# Binding two generic lists to a datagrid view. Failed to enable constraints
<p>We’re binding two static lists to our Grid View. One for Categories and another for Products. If I bind one (doesn't matter which) No problem. If we do two it doesn't work.</p>

<p>What we didn’t know is binding them is not supported.<br>
<a href="https://stackoverflow.com/questions/6407428/bind-multiple-lists-to-a-datagridview">Bind Multiple Lists to a DataGridView</a> </p>

<p>We’ve found a NuGet library called FastMember 1.1.0. It can help us access that generic list with the code to do that.
<a href="https://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable">Convert generic List/Enumerable to DataTable?</a>     </p>

<p>We did it but we’re getting a System.Data.ConstraintException right after we try and load the Category table in our products form. </p>

<blockquote>
  <p>Failed to enable constraints. One or more rows contain values
  violating non-null, unique, or foreign-key constraints</p>
</blockquote>

<p>Three reasons in this article are the likely suspect. </p>

<p><a href="https://stackoverflow.com/questions/7026566/failed-to-enable-constraints-one-or-more-rows-contain-values-violating-non-null">Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints</a> </p>

<p>•   Null values being returned for columns not set to AllowDBNull</p>

<p>•   Duplicate rows being returned with the same primary key.</p>

<p>•   A mismatch in column definition (e.g. size of char fields) between the database and the dataset</p>

<p>I scanned the row being returned and the auto’s window is showing approximately 6 rows, so duplicates aren’t being returned.</p>

<p>I can’t see any constraints being enforced for the price field other than the fact it’s just not allowing nulls, so what? Why should it crash over that? </p>

<p>I don’t feel it’s having a problem with primary keys. </p>

<p>It appears the first bullet point reason is true. Ran this in the command windows after the exception was triggered. </p>

<pre><code>? table.GetErrors()
"Column 'Price' does not allow DBNull.Value."
</code></pre>

<p>Then I went further: </p>

<pre><code>? table.GetErrors()
{System.Data.DataRow[4]}
    [0]: {System.Data.DataRow}
    [1]: {System.Data.DataRow}
    [2]: {System.Data.DataRow}
    [3]: {System.Data.DataRow}
? table.GetErrors()[0].RowError
"Column 'Price' does not allow DBNull.Value."
</code></pre>

<p>I don’t have any nulls for Price in my database. Why should this even matter? And how do I get passed that to populate my gridview with two lists.</p>

<p>This is my webform, ProductList.</p>

<pre><code>namespace PrestoSkateShop
{
    public partial class ProductList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {            
                if (IsPostBack)
                    return;

            //PROBLEM: To get products and categories list and display them on screen.
                //GET Product list 
                //GET Category list
                //CONVERT list to data table
                //COMBINE data tables into one data table
                //Bind Data table to grid view
                //END

                DataTable table = new DataTable();
                IEnumerable&lt;Product&gt; ProductData = Product.GetAllProducts();
                using (var reader = ObjectReader.Create(ProductData))
                {                    
                    table.Load(reader);
                }

                IEnumerable&lt;Category&gt; CategoryData = Category.GetAllCategorys();
                using (var reader = ObjectReader.Create(CategoryData))
                {
                    table.Load(reader);
                }


                gvList.DataSource = table;                
                gvList.DataBind();
        }        
    }
}
</code></pre>

<p>Product Class. </p>

<pre><code>public class Product
    {
        #region Object &amp; Query Declarations
        //Query declarations
        private const string QUERY_SELECT = "SELECT * FROM tblProducts ORDER BY ProductName ASC ";

        #endregion Object &amp; Query Declarations

        #region Properties

        //ID is PK
        public long ID { get; set; }
        public long CategoryID { get; set; }
        public string ProductName { get; set; }        
        public decimal Price { get; set; }
        public string Image { get; set; }
        public string Colour { get; set; }
        public string Size { get; set; }  
        public string Description { get; set; }

        #endregion        

        #region Constructors

        public Product() { }       

        #endregion



        #region Initalizers
        /// &lt;summary&gt;
        /// Loads an entire list of products.
        /// &lt;/summary&gt;
        public static List&lt;Product&gt; GetAllProducts()
        {
            List&lt;Product&gt; listProducts = new List&lt;Product&gt;();
            using (SqlConnection conn = new SqlConnection(Models.Strings.CONNECTION_STRING))
            {
                SqlCommand cmd = new SqlCommand(QUERY_SELECT, conn);
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    Product product = new Product();
                    product.ID = (long)rdr[0];
                    product.CategoryID = (long)rdr[1];
                    product.ProductName = rdr[2].ToString();
                    product.Price = decimal.Parse(rdr[3].ToString());
                    product.Image = rdr[4].ToString();
                    product.Colour = rdr[5].ToString();
                    product.Size = rdr[6].ToString();
                    product.Description = rdr[7].ToString();
                    listProducts.Add(product);
                }
            }
            return listProducts;
        }
        #endregion
    }
}
</code></pre>

<p>Category Class</p>

<pre><code>public class Category
    {

        #region Object &amp; Query Declarations
        //Query declarations
        private const string QUERY_SELECT = "SELECT * FROM tblCategories ORDER BY CategoryName ASC ";


        #endregion Object &amp; Query Declarations

        #region Properties

        //ID is PK
        public long ID { get; set; }
        public string CategoryName { get; set; }       

        #endregion        

        #region Constructors

        public Category() { }       

        #endregion

        #region Initalizers
        /// &lt;summary&gt;
        /// Loads an entire list of Categorys.
        /// &lt;/summary&gt;
        public static List&lt;Category&gt; GetAllCategorys()
        {
            List&lt;Category&gt; listCategorys = new List&lt;Category&gt;();
            using (SqlConnection conn = new SqlConnection(Models.Strings.CONNECTION_STRING))
            {
                SqlCommand cmd = new SqlCommand(QUERY_SELECT, conn);
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    Category Category = new Category();
                    Category.ID = (long)rdr[0];
                    Category.CategoryName = rdr[1].ToString();
                    listCategorys.Add(Category);
                }
            }
            return listCategorys;
        }
        #endregion
    }
}
</code></pre>

<p>SQL Server database Images </p>

<p><a href="https://i.stack.imgur.com/HCkh7.png" rel="nofollow noreferrer">SQL Server Database table design</a></p>

<p><a href="https://i.stack.imgur.com/zKvFL.png" rel="nofollow noreferrer">SQL Server Database Table data</a></p>