Rekcs Rekcs - 2 years ago 106 Question

Link combobox with database values

I'm trying to link a ComboBox with Database values but after I debug it gave me an error related System.IndexOutOfRangeException. Here it is the following code that I'm using right now

SQLCon = New SqlConnection
SQLCon.ConnectionString = "..."
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
SQLDataReader = SqlCmd.ExecuteReader
While SQLDataReader.Read
Dim fileType = SQLDataReader.GetString(1)
End While
Catch ex As SqlException
End Try

The error occurs
Dim fileType = SQLDataReader.GetString(1)

I know why this is happening, because if I use the
SELECT * FROM infofile
it works but I want to group it instead of displays all the data

Answer Source

GetString() takes a 0-based index number. You are asking it to give you the second element (index number 1) when you only selected one column. There is no second element. Change the 1 to a 0.

More info on the GetString method:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download