我正在尝试写一些可以在工作中使用并且正在自学的东西。我遇到了一个我找不到解决方案的问题,尽管我很可能只是不熟悉正确的术语。
我已将 Visual Studio 项目附加到 SQL 数据库,并尝试执行搜索,我可以在三个文本框中的任何一个中输入信息,它将相应地过滤返回的结果。这是用户搜索查询:
SELECT First_Name AS [First Name], Middle_Name AS [Middle Name], Last_Name AS [Last Name], Users_ID AS [Unique ID]
FROM Users
WHERE (First_Name LIKE '%' + @firstName + '%') AND (Last_Name LIKE '%' + @lastName + '%') AND (Users_ID LIKE '%' + @uniqueID + '%')
ORDER BY [Last Name]
这是我用来调用搜索的 SUB。我唯一遇到的问题是用户搜索。
Private Sub searchButton_Click(sender As Object, e As EventArgs) Handles searchButton.Click
'Clear all variables when the search button is clicked. This allows Search to start with a clean'
'slate and gather new or changed variables again.'
addRemoveValue = Nothing
jobTitle = Nothing
jtLocation = Nothing
jtDepartment = Nothing
loc = Nothing
lComments = Nothing
department = Nothing
fname = Nothing
lname = Nothing
uID = Nothing
'Gather variables.'
addRemoveValue = TryCast(addRemoveComboBox.SelectedItem, String)
jobTitle = TryCast(jobTitleTextBox.Text, String)
jtLocation = TryCast(jtLocationComboBox.Text, String)
jtDepartment = TryCast(jtDepartmentComboBox.Text, String)
loc = TryCast(locationTextBox.Text, String)
lComments = TryCast(lCommentsTextBox.Text, String)
department = TryCast(departmentTextBox.Text, String)
fname = TryCast(firstNameTextBox.Text, String)
lname = TryCast(lastNameTextBox.Text, String)
uID = TryCast(uniqueIDTextBox.Text, String)
'I added this in order to troubleshoot the search feature for the Users search.'
'If uID = Nothing Then'
' uID = "%"'
'End If'
'Change the visible buttons in the event of a Search.'
disableButton.Visible = True
addButton.Visible = False
'Populate the Data Grid based on the item the user selects.'
If addRemoveValue = "Job Title" Then
With Me.DataGridView1
.AutoGenerateColumns = True
.DataSource = RoleTableAdapter.GetDataByTitle(jobTitle, jtLocation, jtDepartment)
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.DisplayedCells
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
End With
End If
If addRemoveValue = "Location" Then
With Me.DataGridView1
.AutoGenerateColumns = True
.DataSource = LocationTableAdapter.GetDataByLoc(loc)
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
End With
End If
If addRemoveValue = "Department" Then
With Me.DataGridView1
.AutoGenerateColumns = True
.DataSource = DepartmentTableAdapter.GetDataByDept(department)
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
End With
End If
If addRemoveValue = "User" Then
With Me.DataGridView1
.AutoGenerateColumns = True
.DataSource = UsersTableAdapter.GetDataByName(uID, fname, lname)
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
.Columns("Unique ID").DisplayIndex = 0
.Columns("Last Name").DisplayIndex = 1
.Columns("First Name").DisplayIndex = 2
.Columns("Middle Name").DisplayIndex = 3
End With
End If
End Sub
每当我对此进行测试时,都会收到以下错误:
无法启用约束。一行或多行包含违反非空、唯一或外键约束的值。
我查看了所有列的属性,它们都可以包含 NULL 值。我已经查看并确保我的任何表上都没有外键约束。我不知道接下来要寻找什么来解决这个问题,因为我在这段代码中进行的其他三个搜索工作得很好,而且他们有非常相似的查询。
你能提供的任何帮助都会很棒!谢谢!!