无法设置列'名称'。该值违反此列的MaxLength限制

时间:2013-12-18 14:05:20

标签: vb.net visual-studio datagridview datatable

我有一个包含5列的表格。

他们是:

fItemID - Int IDENTITY主键
fItemName - varchar(50)
fItemType - varchar(50)
fItemModel - varchar(50)
fItemWeight - int

当我尝试使用DataGridView向表中添加数据时,只要我将值保持在10个字符以下,我就没有问题,但是当我尝试添加11个或更多字符时,我收到三个错误消息列。

对于 fItemName fItemType ,我得到:

  

无法设置列'名称'。该值违反了此列的MaxLength限制。

当我尝试向 fItemWeight 添加超过10个字符时,我得到:

  

System.FormatException:对于Int32,值太大或太小。 ---> System.OverFlowException:对于Int32,值太大或太小。

我不知道为什么我会收到这些错误,因为向 fItemModel 添加信息效果非常好。

像往常一样,我使用VS2013 Ultimate和VB.Net。


以下是表单的所有代码:

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

以下是我的表的代码:

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)
);

3 个答案:

答案 0 :(得分:4)

我认为您的适配器与数据库同步。您可以尝试重新映射,也可以尝试在加载方法中附加此内容:

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

<强>重映射

向项目添加数据库(数据库)时VS(Visual Studio)会创建表示数据的强类型内存缓存。这使您可以使用与数据库相同的模式在应用程序中使用DataSet。但是,如果数据库架构随后发生更改(例如,您更改了列的类型/长度/名称),则缓存更新。在“解决方案资源管理器”窗格中,应该有一个名为[NameOfDB]DataSet.xsd的文件。双击此文件将打开一个编辑器,允许您编辑DS及其所有表。确保它反映更新的数据库架构 - 关闭 - 保存 - 重建。

答案 1 :(得分:1)

我知道这是一个老帖子,但对于那些在类似问题上挣扎的人:

我通过简单地从项目资源管理器中删除数据集并通过&#34;添加新数据源&#34;重新添加数据集来解决这个问题。项目菜单中的选项。只要您不删除位于表单编辑器底部的表引用,您的数据绑定就可以了。

重新添加数据集时,必须将其命名为与删除数据集之前完全相同的名称。如果操作正确,数据库中任何已编辑的列现在都将恢复同步。

答案 2 :(得分:0)

**第一次需要从DB

更改表格中的maxlength

试试这个(在-VS 2010 - )

转到解决方案资源管理器

点击'YourFile'.xsd / open

右键

搜索&amp;在冲突中的列名称上单击右侧

属性

根据需要更改MaxLength

保存