Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted

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
 
Share this answer
 
v2
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:

SQL
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
 
Share this answer
 
Sujith Karivelil811 Thank you sir, your query works on my project, thank you so much =)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900