Public Sub AutoSuggestDropDownListFromDB(ByVal cboName As Elegant.Ui.ComboBox, ByVal TableName As String, ByVal ColumnName As String)
'TODO
Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
'Specify an SQL query.
Dim sSQL As String = "SELECT " & ColumnName & " FROM " & TableName
'Assign the SQL query and database connection to the DataAdapter.
Dim MyDataAdapter As New SqlDataAdapter(sSQL, SetDatabaseConnection)
'Create a new instance of a DataSet.
Dim MyDataSet As New DataSet
MyDataSet.Locale = System.Globalization.CultureInfo.CurrentCulture
Try
'Clear the DataSet of any previously obtained database information.
MyDataSet.Clear()
'Refill the DataAdapter with new information obtained from the database.
MyDataAdapter.Fill(MyDataSet, TableName)
'Fill the ComboBox.
With cboName
'Assign the database table to the DataSource property.
.DataSource = MyDataSet.Tables(TableName)
'Assign the database column name to the DisplayMember and ValueMember properties.
'.DisplayMember = ColumnName
'.ValueMember = ColumnName
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
Catch ex As SqlException
'Show the end user a message if an error is generated.
MessageBox.Show("There was an error retrieving data from table name " & TableName & " for the column " & ColumnName & "." & vbNewLine & vbNewLine & _
"We are going to prepopulate the dropdown menu with a default value; therefore, you can disregard the error." & vbNewLine & vbNewLine & _
"We highly suggest that you click on the plus sign next to the dropdown menu to add some options of your own." & vbNewLine & vbNewLine & _
"The reason why you got this error in the first place is:" & vbNewLine & _
"No data items in the database " & TableName & " to pull." & vbNewLine & vbNewLine & _
"The exception error is:" & vbNewLine & _
ex.Message, _
"Database Information Retrieval Issues", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly, False)
Catch ex As ArgumentOutOfRangeException
MessageBox.Show(ex.Message, _
"Argument Out Of Range Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly, False)
End Try
End Using 'SetDatabaseConnection.
End Sub