无法将值NULL插入列'intGolferEventYearID',表格不允许为空

时间:2019-04-15 20:48:52

标签: sql-server vb.net visual-studio

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

它有两个按钮。一个将高尔夫球手从“ inEvent”中删除,并将其移至“可用”。此按钮有效。 另一个按钮则相反。它将可用的高尔夫球手添加到选定的赛事年份。但这给了我错误- “该语句已终止。无法将值NULL插入表'dbo.TGolferEventYears'的列'intGolferEventYearID'中;该列不允许为空。插入失败。”

在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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 & ")"