vb.net 无法设置列“名称”。该值违反了此列的 MaxLength 限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20660286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot set Column 'name'. The value violates the MaxLength limit of this column
提问by ZLHysong
I have a table with 5 columns.
我有一个有 5 列的表格。
They are:
他们是:
fItemID- Int IDENTITY Primary Key
fItemName- varchar(50)
fItemType- varchar(50)
fItemModel- varchar(50)
fItemWeight- int
fItemID- Int IDENTITY 主键
fItemName- varchar(50)
fItemType- varchar(50)
fItemModel- varchar(50)
fItemWeight- int
When I try to add data to the table, using a DataGridView, I have no problems as long as I keep the values under 10 characters, but when I try to add 11 or more, I get an error message on three of the columns.
当我尝试使用 DataGridView 将数据添加到表中时,只要将值保持在 10 个字符以下,我就没有问题,但是当我尝试添加 11 个或更多字符时,我在其中三个列中收到错误消息。
For fItemNameand fItemType, I get:
对于fItemName和fItemType,我得到:
Cannot set Column 'name'. The value violates the MaxLength limit of this column.
无法设置列“名称”。该值违反了此列的 MaxLength 限制。
When I try to add more then 10 characters to fItemWeight, I get:
当我尝试向fItemWeight添加超过 10 个字符时,我得到:
System.FormatException: Value was either too large or too small for an Int32. ---> System.OverFlowException: Value was either too large or too small for an Int32.
System.FormatException: 对于 Int32,值太大或太小。---> System.OverFlowException: 值对于 Int32 来说太大或太小。
I have no idea why I am getting these errors, as adding info to fItemModelworks perfectly.
我不知道为什么会出现这些错误,因为向fItemModel添加信息非常有效。
As usual, I am using VS2013 Ultimate and VB.Net.
像往常一样,我使用 VS2013 Ultimate 和 VB.Net。
The following is all the code for the form:
以下是表格的全部代码:
Public Class frmBOMNonSteel
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DbStarFliteSystemsDataset.tblItemsQuery' table. You can move, or remove it, as needed.
Me.TblItemsQueryTableAdapter.Fill(Me.DbStarFliteSystemsDataset.tblItemsQuery)
'TODO: This line of code loads data into the 'DbStarFliteSystemsDataset.tblItems' table. You can move, or remove it, as needed.
Me.TblItemsTableAdapter.Fill(Me.DbStarFliteSystemsDataset.tblItems)
'TODO: This line of code loads data into the 'DbStarFliteSystemsDataset.tblItems' table. You can move, or remove it, as needed.
Me.TblItemsTableAdapter.Fill(Me.DbStarFliteSystemsDataset.tblItems)
'TODO: This line of code loads data into the 'DbStarFliteSystemsDataset.tblItems' table. You can move, or remove it, as needed.
Me.TblItemsTableAdapter.Fill(Me.DbStarFliteSystemsDataset.tblItems)
'TODO: This line of code loads data into the 'DbStarFliteSystemsDataset.tblItems' table. You can move, or remove it, as needed.
Me.TblItemsTableAdapter.Fill(Me.DbStarFliteSystemsDataset.tblItems)
Me.txtJobNumber.Text = frmParent.ToolStripTextBox1.Text
With FItemTypeComboBox
.DataSource = DbStarFliteSystemsDataset.tblItemsQuery
.ValueMember = "fItemType"
End With
End Sub
Private Sub SteelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SteelToolStripMenuItem.Click
Me.Close()
frmBOMSteel.MdiParent = frmParent
frmBOMSteel.Show()
frmBOMSteel.WindowState = FormWindowState.Maximized
End Sub
Private Sub NonSteelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NonSteelToolStripMenuItem.Click
Me.MdiParent = frmParent
Me.Show()
End Sub
Private Sub WeightsRevisionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WeightsRevisionsToolStripMenuItem.Click
Me.Close()
frmBOMWeightsRevisions.MdiParent = frmParent
frmBOMWeightsRevisions.Show()
frmBOMWeightsRevisions.WindowState = FormWindowState.Maximized
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
frmModalNewItem.ShowDialog()
End Sub
Private Sub FItemTypeComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FItemTypeComboBox.SelectedIndexChanged
Me.FItemNameComboBox.Text = ""
Dim selected As Object = Me.FItemTypeComboBox.SelectedItem
If (TypeOf selected Is DataRowView) Then
Dim row As DataRow = DirectCast(selected, DataRowView).Row
Dim fItemType As String = CStr(row.Item("fItemType"))
Dim view As New DataView(DbStarFliteSystemsDataset.tblItems)
view.RowFilter = String.Format("[fItemType]='{0}'", fItemType)
Me.FItemNameComboBox.DataSource = view.ToTable(True, "fItemName")
Me.FItemNameComboBox.DisplayMember = "fItemName"
End If
End Sub
Private Sub TblItemsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.TblItemsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DbStarFliteSystemsDataset)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.TblItemsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DbStarFliteSystemsDataset)
End Sub
Private Sub TblItemsBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs)
Me.Validate()
Me.TblItemsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DbStarFliteSystemsDataset)
End Sub
Private Sub TblItemsBindingNavigatorSaveItem_Click_2(sender As Object, e As EventArgs)
Me.Validate()
Me.TblItemsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DbStarFliteSystemsDataset)
End Sub
End Class
The following is the code for my Table:
以下是我的表的代码:
CREATE TABLE [dbo].[tblItems] (
[fItemID] INT IDENTITY (1, 1) NOT NULL,
[fItemName] VARCHAR (50) NULL,
[fItemType] VARCHAR (50) NULL,
[fItemModel] VARCHAR (50) NULL,
[fItemWeight] INT NULL,
CONSTRAINT [PK_tblItems] PRIMARY KEY CLUSTERED ([fItemID] ASC)
);
回答by Bj?rn-Roger Kringsj?
I believe that your adapters are notin sync with the database. You could try remapping or you could try appending this within the load method:
我相信您的适配器与数据库不同步。您可以尝试重新映射,也可以尝试在 load 方法中附加它:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.DbStarFliteSystemsDataset.tblItems.Columns("fItemName").MaxLength = 50
Me.DbStarFliteSystemsDataset.tblItems.Columns("fItemType").MaxLength = 50
Me.DbStarFliteSystemsDataset.tblItems.Columns("fItemModel").MaxLength = 50
'.....
End Sub
Remapping
重新映射
When you add a DB (database) to your project VS (Visual Studio) creates a strongly typed in-memory cache representing the data. This enables you to use a DataSetin your application with the same schema as your DB. However, if the DB schema changes afterwards (e.g. you have changed a type/length/name of a column), the cache is notupdated. In the "Solution Explorer" pane there should be a file named [NameOfDB]DataSet.xsd. Double-clicking this file will open an editor allowing you to edit the DS and all its tables. Ensure it reflects the updated DB schema - close - save - rebuild.
当您将 DB(数据库)添加到您的项目时,VS (Visual Studio) 会创建一个表示数据的强类型内存缓存。这使您能够DataSet在您的应用程序中使用与您的数据库相同的架构。但是,如果数据库架构随后发生更改(例如,您更改了列的类型/长度/名称),则缓存不会更新。在“解决方案资源管理器”窗格中应该有一个名为[NameOfDB]DataSet.xsd. 双击该文件将打开一个编辑器,允许您编辑 DS 及其所有表。确保它反映更新的数据库架构 - 关闭 - 保存 - 重建。
回答by SilverShotBee
I know this is an old post, but for those struggling with a similar issue:
我知道这是一个旧帖子,但对于那些在类似问题上挣扎的人:
I resolved this by simply deleting the dataset out of the project explorer and re-adding it via the "Add New Data Source" option in the project menu. As long as you don't delete the table references that sit at the bottom of the form editor, you're databindings will be OK.
我通过简单地从项目资源管理器中删除数据集并通过项目菜单中的“添加新数据源”选项重新添加它来解决这个问题。只要您不删除位于表单编辑器底部的表引用,您就可以进行数据绑定。
When you re-add the dataset, it must be called the exact same name as the one you had before you deleted it. If done correctly, any edited columns in the database will now be back in sync.
重新添加数据集时,它的名称必须与删除前的名称完全相同。如果操作正确,数据库中任何已编辑的列现在都将恢复同步。
回答by user3220555
**in the first time you need change the maxlength in your table from DB
**第一次需要从数据库更改表中的最大长度
try this ( in -VS 2010-)
试试这个(在-VS 2010-)
Go to Solution Explorer
转到解决方案资源管理器
click right on 'YourFile'.xsd/ open
右键单击“YourFile”.xsd/ 打开
Search & click right on the Column name in conflict
在冲突的列名上搜索并右键单击
Properties
特性
change MaxLength to your need
将 MaxLength 更改为您的需要
Save
节省

