System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
at System.Data.DataTable.EnableConstraints()
at System.Data.DataTable.set_EnforceConstraints(Boolean value)
at System.Data.DataTable.EndLoadData()

遇到这个错误 Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
请参考msdn文章来debug找出错误的地方:  http://msdn.microsoft.com/en-us/library/system.data.datatable.geterrors(VS.71).aspx
它检查DataSet中的所有DataTable的HasErrors属性,然后对有错误的DataTable使用GetErrors方法。
GetErrors返回一组DataRows,检查返回的所有DataRow的RowError属性来获得确切的错误信息。

DataTable dataTable = new DataTable()
int returnValue = ;
try
{
returnValue = this.Adapter.Fill(dataTable);
}
catch (System.Data.DataException e)
{
System.Data.DataRow[] rowsInError;
System.Text.StringBuilder sbError = new System.Text.StringBuilder();
// Test if the table has errors. If not, skip it.
if (dataTable.HasErrors)
{
// Get an array of all rows with errors.
rowsInError = dataTable.GetErrors();
// Print the error of each column in each row.
for (int i = ; i < rowsInError.Length; i++)
{
foreach (System.Data.DataColumn column in dataTable.Columns)
{
sbError.Append(column.ColumnName + " " + rowsInError[i].GetColumnError(column));
}
// Clear the row errors
rowsInError[i].ClearErrors();
}
}
string time = System.DateTime.Now.ToString();
var fileName = "Error.log";
string filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
System.IO.FileStream fst = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
System.IO.StreamWriter swt = new System.IO.StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
swt.WriteLine("======================");
swt.WriteLine(time);
swt.WriteLine("----------------------");
swt.WriteLine(sbError.ToString());
swt.WriteLine("----------------------");
swt.WriteLine(e.ToString());
swt.Close();
fst.Close();
}
return returnValue;

具体错误将显示在日志中

======================
2/27/2014 11:47:54 PM
----------------------
CustomerID ParentCustomerID CustomerNumber Column 'CustomerNumber' exceeds the MaxLength limit.Name StatementName Inactive OnHold EMailAddress TimeStamp ShippingMethodID PriceLevel TradeDiscount TaxExempt ClassID UserDefine1 UserDefine2 CreditLimitType TimeStamp1 ActivationCode
----------------------
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
at System.Data.DataTable.EnableConstraints()
at System.Data.DataTable.set_EnforceConstraints(Boolean value)
at System.Data.DataTable.EndLoadData()
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at Nodus.Applications.EPay.Data.EntitiesTableAdapters.CustomerTableAdapter.FillByKeyword(CustomerDataTable dataTable, String Keyword, Nullable`1 MaxResults)

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.的更多相关文章

  1. InnoDB和Foreign KEY Constraints

    InnoDB表中中Foreign Key定义 1. InnoDB允许a foreign key引用一个索引列或者索引组列. 2. InnoDB现在并不支持用户定义的分区表有foreign keys,这 ...

  2. 解决Windows8下Cisco Systems VPN Client的Reason 442: Failed to Enable Virtual Adapter错误

    Windows8下使用Cisco Systems VPN Client创建的Cisco IPSec VPN无法连接,提示Reason 442: Failed to Enable Virtual Ada ...

  3. WIN8 下Cisco VPN连接 出现vpn 422 failed to enable virtual adapter错误

    今天在家用VPN软件连接,出现了“vpn 422 failed to enable virtual adapter”的错误,系统安装的是Win8专业版32位,百度了半天又很多方法解决不了,后来发现了一 ...

  4. 解决Cisco VPN Client:Reason 442: Failed to Enable Virtual Adapter VPN连接问题

    大公司里肯定涉及不同地点的办公问题,这样VPN的使用就频繁了,今天遇到一个VPN连接问题,分享给大家,看一眼,以后不在这问题上耗费太多功夫. 在win7上连接vpn时抛出“failed to enab ...

  5. Windows 8 – Reason 442: Failed to enable Virtual Adapter

    Cisco VPN on Windows 8.1 – Reason 442: Failed to enable Virtual Adapter https://supertekboy.com/2013 ...

  6. Composer\Downloader\TransportException ... Failed to enable crypto,failed to open stream: operation failed

    failed to open stream: operation failed 错误详细信息: [Composer\Downloader\TransportException] The "h ...

  7. Foreign key (referential) constraints on DB2 LUW v105

    oreign key constraints (also known as referential constraints or referential integrity constraints) ...

  8. Azure CosmosDB (9) Unique Key Constraints

    <Windows Azure Platform 系列文章目录> 在Azure Cosmos DB中,还支持Unique Key Constraints(唯一键约束). 我们可以在Azure ...

  9. Sqlite: FOREIGN KEY constraint failed on remove的错误原因。

    sqlite外键约束中.restrict约束:如果要删除父表,则子表需空. 如果没有定义约束.会报错,需设置一个约束.

随机推荐

  1. 树莓派进阶之路 (015) - 树莓派使用DS18B20模块测量温度

    参考:http://shumeipai.nxez.com/2013/10/03/raspberry-pi-temperature-sensor-monitors.html 第一步,允许单总线接口 su ...

  2. IOC 之深入理解 Spring IoC

    在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...

  3. k8s源码分析之kubelet

    一.概述 二.Kubelet对象创建过程:(pkg/kubelet/kubelet.go ) NewMainKubelet 正如名字所示,主要的工作就是创建 Kubelet 这个对象,它包含了 kub ...

  4. 使用Delphi实现票据精确打印

    一.概述在银行,税务,邮政等行业的实际工作中,经常涉及到在印刷好具有固定格式的汇款单,储蓄凭证,税票等单据上的确定位置打印输出相关的信息.在此类需求中,精确地定位单据并打印相关信息,是解决问题的关键. ...

  5. Windows10内置Linux子系统

      WSL 前言 前段时间,机子上的win10又偷偷摸摸升级到了一周年正式版,比较无奈.不过之前听闻这个版本已经支持内置的linux子系统,于是就怀着好奇心试玩了一把.虽然期间遇到了很多问题,但总体来 ...

  6. Digital Color Meter 颜色值提取工具

    1.Digital Color Meter 简介 Digital Color Meter 是一款 Mac 自带的颜色值提取工具. 其它下载地址 Digital Color Meter for Mac, ...

  7. High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件

    原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control 本文翻译在CodeProject上的介绍(主要还 ...

  8. 像网页开发一样调试ios程序

    PonyDebugger https://github.com/square/PonyDebugger

  9. 开关电源9v,1A

  10. Android工具类-关于网络、状态的工具类

    下方是一个很好的监测网络.状态的工具类 public class NetworkUtils { /** * 网络是否可用 * * @param activity * @return */ public ...