首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

急未能起用约束。一行或多行中包含违反非空、唯一或外键约束的值

2013-04-20 
急:未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值代码如下:Private Sub Button1_Click(ByVal

急:未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值
代码如下:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newsql As String = "select ccuscode,ccusname,ccusabbname  from customer where ccuscode=" & TextBox1.Text
        Me.CustomerTableAdapter.Adapter.SelectCommand.CommandText = newsql
        Me.CustomerTableAdapter.Fill(Me.UFDATA_008_2012DataSet.Customer)
    End Sub

错误在倒数第二行
提示如下:



未处理 System.Data.ConstraintException
  Message=未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
  Source=System.Data
  StackTrace:
       在 System.Data.DataSet.FailedEnableConstraints()
       在 System.Data.DataSet.EnableConstraints()
       在 System.Data.DataSet.set_EnforceConstraints(Boolean value)
       在 System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       在 System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
       在 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
       在 WindowsApplication1.UFDATA_008_2012DataSetTableAdapters.CustomerTableAdapter.Fill(CustomerDataTable dataTable) 位置 C:\Documents and Settings\Administrator\My Documents\VB.NET\dzd\dzd\UFDATA_008_2012DataSet.Designer.vb:行号 6182
       在 WindowsApplication1.Form2.Button1_Click(Object sender, EventArgs e) 位置 C:\Documents and Settings\Administrator\My Documents\VB.NET\dzd\dzd\Form2.vb:行号 19
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(ApplicationContext context)
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       在 WindowsApplication1.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
vb.net错误
[解决办法]
有一个字段你没有指定值,而你在数据库中有不允许它为空。或者这个字段必须唯一,而你插入的时候,已经有记录和它重复了。或者它指向一个外键,而那个外键值无效。
[解决办法]
Dim newsql As String = "select ccuscode,ccusname,ccusabbname  from customer where ccuscode='"& TextBox1.Text &"'"

热点排行