无法在列'intGolferEventYearID'中插入值NULL,表不允许空值

问题描述 投票:0回答:2

我有一个组合框的对话框列出了举办活动的年份。更改年份,更改以下列表框一个名为“inEvent”的列表框显示所有高尔夫球手参加的所述事件。另一个名为“可用”的列表框显示我们在数据库中没有参加该年度活动的每个高尔夫球手

它有两个按钮。一个人从'inEvent'移除高尔夫球手并将他们移动到'可用'。这个按钮有效。另一个按钮则相反。它为所选活动年度增加了可用的高尔夫球手。但它给了我错误 - “语句已被终止。无法将值NULL插入列'intGolferEventYearID',表'dbo.TGolferEventYears';列不允许空值.INSERT失败。”

更改VB中的任何代码行会导致不同的错误。所以我认为错误必须来自SQL本身,我不太了解。只有我能想到的其他事情是列表框提供了错误的信息。

Private Sub btnAddAuto_Click(sender As Object, e As EventArgs) Handles btnAddAuto.Click

    Dim strInsert As String = ""
    Dim cmdInsert As OleDb.OleDbCommand ' used for our Select statement
    Dim dt As DataTable = New DataTable ' table we will load from our reader
    Dim intRowsAffected As Integer

    ' open the DB
    OpenDatabaseConnectionSQLServer()

    ' Build the select statement
    strInsert = "INSERT INTO TGolferEventYears ( intGolferID, intEventYearID) Values (" & lstAvailable.SelectedValue & ", " & cboEvents.SelectedIndex + 1 & ")"

    ' Retrieve all the records 
    cmdInsert = New OleDb.OleDbCommand(strInsert, m_conAdministrator)
    intRowsAffected = cmdInsert.ExecuteNonQuery()

    ' close the database connection and reload the form so the changes are shown
    CloseDatabaseConnection()
    frmEventsGolfers_Load(sender, e)
End Sub
sql-server vb.net visual-studio
2个回答
0
投票

我将数据访问代码与用户界面分开。如果您以后愿意,这将使您可以轻松地从表单中删除数据访问。

Private Sub MoveGolfer(GolfID As Integer, YearID As Integer)
    'If you keep your connections local you can be sure they are 
    'closed and disposed which is what the Using...End Using blocks do.
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("INSERT INTO TGolferEventYears ( intGolferID, intEventYearID) Values (@Golf, @Year;")
            'Always use parameters to protect against Sql injection
            cmd.Parameters.Add("@Golf", SqlDbType.Int).Value = GolfID
            cmd.Parameters.Add("@Year", SqlDbType.Int).Value = YearID
            cn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim GolferID As Integer = CInt(lstAvailable.SelectedValue)
    Dim Year As Integer = cboEvents.SelectedIndex + 1
    Try
        MoveGolfer(GolferID, Year)
    Catch ex As Exception
        'Catch a more specific exception and handle accordingly
        MessageBox.Show(ex.Message)
    End Try
End Sub

0
投票

由于我不太了解SQL,我没有意识到我的PK没有在表中添加IDENTITY标记。添加此修复我的问题。

这是我添加的代码

        Dim strSelect As String
        Dim cmdSelect As OleDb.OleDbCommand            
        Dim drSourceTable As OleDb.OleDbDataReader
        Dim intNextHighestRecordID As 

        strSelect = "SELECT MAX(intDealerAutos) + 1 AS intNextHighestRecordID " &
                        " FROM TDealerAutos"

        'Excute command
        cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
        drSourceTable = cmdSelect.ExecuteReader

        'Read result( highest ID )
        drSourceTable.Read()

        'Null? (Empty Table)
        If drSourceTable.IsDBNull(0) = True Then
            'Yes, start numbering at 1
            intNextHighestRecordID = 1
        Else
            'No, get the next highest ID
            intNextHighestRecordID = CInt(drSourceTable.Item(0))
        End If

        'Build the select statement
        strInsert = "INSERT INTO TDealerAutos ( intDealerAutos, intDealerID, intAutoID)" &
                    "Values (" & intNextHighestRecordID & ", " & cboDealers.SelectedValue & ", " & lstAvailable.SelectedValue & ")"
© www.soinside.com 2019 - 2024. All rights reserved.