I have tried everything I can think of and it’s not working. If I can’t get a simple thing to work then I am trashing that form altogether!
This is the code I am using to populate the form and then I am trying to get the defaulting done from the SAME code. It isn’t doing anything!
<CLSCompliant(True)> Public Sub PopulateDropDownListFromDB(ByVal cboName As Elegant.Ui.ComboBox, _
ByVal TableName As String, ByVal ColumnName As String, _
ByVal UseAutoComplete As Boolean, _
ByVal UseSQLDistinct As Boolean)
Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
’Specify an SQL query.
Dim sSQL As String = Nothing
If UseSQLDistinct Then
’NOTES: frmReportSelection has some dupes from the DB, use DISTINCT in the SQL.
sSQL = "SELECT DISTINCT " & ColumnName & " FROM " & TableName
Else
sSQL = "SELECT " & ColumnName & " FROM " & TableName
End If
’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 = 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)
With cboName
’First check to see if any records. If no records, add a default item.
Dim MyDataTable As DataTable = New DataTable
MyDataTable.Locale = CurrentCulture
MyDataTable = MyDataSet.Tables(TableName)
’Fill the ComboBox.
’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
If UseAutoComplete = True Then
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End If
’Take the value from cboPartnerName on frmWorkLogEntryStart and default it on frmWorkLogEntry.
If .Name Is "cboPartnername" Then
With frmWorkLogEntryStart
frmWorkLogEntry.cboPartnerName.SelectedItem = .cboPartnerName.Items(.cboPartnerName.FindStringExact(.cboPartnerName.Text, 0))
End With
End If
If MyDataTable.Rows.Count > 0 Then ’Has records.
If .Editable = False _
And UseAutoComplete = False Then
’Specify a default selection in the ComboBox.
.SelectedIndex = 0
End If
Else
.DataSource = Nothing
.Items.Add("[Please Add An Item]")
.SelectedIndex = 0
End If
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)
’If no rows are returned to populate the ComboBox, this will return an error, prepopulate with [Please Add An Item]
With cboName
.DataSource = Nothing
.Items.Add("[Please Add An Item]")
.SelectedIndex = 0
End With
End Try
End Using ’SetDatabaseConnection.
End Sub