显示连接了3个表的组合框中的名称 [英] Display Names in a combo box with 3 tables connected
问题描述
我收到此错误System.ArgumentOutOfRangeException:InvalidArgument ='0'的值对'SelectedIndex'无效
我希望你能帮助我。
先谢谢。
调用conecDB()
尝试
strSQL =SELECT Department.Department_Name& _
FROM Center INNER JOIN Division ON Center.Center_ID = Division.Center_ID INNER JOIN Department ON Division.Division_ID = Department.Division_ID& _
WHERE(Department.Dep_Status = 1)& _
订购BY Department_Name ASC
daDB =新的SqlClient.SqlDataAdapter(strSQL,conn)
daDB.Fill(dsDB,Department )
with cmbDepartment
.DataSource = dsDB.Tables(Department,Division,Center)
.ValueMember =Department_ID
.DisplayMember =Department_Name
.SelectedIndex = 0
结束
Catch ex As Exception
MsgBox(ex.ToString)
结束尝试
I got this error "System.ArgumentOutOfRangeException:InvalidArgument=Value of '0' is not valid for 'SelectedIndex' "
I hope you can help me.
Thanks in advance.
Call conecDB()
Try
strSQL = "SELECT Department.Department_Name " & _
"FROM Center INNER JOIN Division ON Center.Center_ID = Division.Center_ID INNER JOIN Department ON Division.Division_ID = Department.Division_ID " & _
"WHERE (Department.Dep_Status = 1) " & _
"Order BY Department_Name ASC "
daDB = New SqlClient.SqlDataAdapter(strSQL, conn)
daDB.Fill(dsDB, "Department")
With cmbDepartment
.DataSource = dsDB.Tables("Department, Division, Center")
.ValueMember = "Department_ID"
.DisplayMember = "Department_Name"
.SelectedIndex = 0
End With
Catch ex As Exception
MsgBox(ex.ToString)
End Try
推荐答案
在数据库1st上执行查询以检查是否获得任何值,如果没有结果集,则组合框将为空。你应该检查组合框中是否有任何数据。
希望它有帮助
execute your query on the database 1st to check if you get any value on them, if there is no result set then your combo box will be empty. you should check the combo box whether there is any data in it or not.
hope it helps
你收到错误是因为它们在 0索引。
为避免这种情况:
1 。检查查询是否在所有条件中返回值,因为您指定了 .ValueMember =Department_ID但是您没有在查询中获取Department_ID的值na?
2.在代码中进行以下更改:
You are getting the error because their is nothing in 0th index of the combobox, when you trying to set the index to 0.
To avoid this :
1. check whether the query returns value in all conditions since you are assigning .ValueMember = "Department_ID" but you are not fetching the value for "Department_ID"in your query na?
2. make the following changes in your code:
Try
trSQL = "SELECT Department.Department_Name," & _
" Department.Department_ID " & _
" FROM Center INNER JOIN Division ON Center.Center_ID=" & _
" Division.Center_ID INNER JOIN Department ON " & _
" Division.Division_ID = Department.Division_ID " & _
" WHERE (Department.Dep_Status = 1) " & _
" Order BY Department_Name ASC "
daDB = New SqlClient.SqlDataAdapter(strSQL, conn)
daDB.Fill(dsDB, "Department")
With cmbDepartment
.DataSource = dsDB.Tables("Department")
.ValueMember = "Department_ID"
.DisplayMember = "Department_Name"
End With
If cmbDepartment.Items.Count > 0 Then
cmbDepartment.SelectedIndex=0
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Sujith Karivelil811谢谢你先生,你的查询适用于我的项目,非常感谢你=)
Sujith Karivelil811 Thank you sir, your query works on my project, thank you so much =)
这篇关于显示连接了3个表的组合框中的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!