VB.NET 2005 - 无法绑定到新的显示成员 - ComboBox - 数组

时间:2014-12-07 05:18:12

标签: sql-server vb.net combobox

所有

我使用存储过程使用INNER JOIN从SQL Server 2005数据库获取记录。我正在使用VB.NET 2005.我将一个参数传递给存储过程,然后将结果加载到字符串列表中,然后用该字符串列表填充ComboBox。

我得到的错误是:

  

参数异常System.ArgumentException复杂数据绑定   接受IList或IListSource的数据源。在   System.Windows.Forms.ListControl.set_DataSource(Object value)at   NotaryTrackIT.GlobalStuff.PopulateDropDownList2(组合框和放大器;控件名称,   String TheState,Boolean AutoComplete)in   C:\用户\ ADMIN \桌面\ NotaryTrackIT \ NotaryTrackIT \ NotaryTrackIT \模块\ GlobalStuff.vb:行   182。

这就是场景。我有一个ComboBox,其中包含来自数据库的美国州代码。这完全没问题。当光标移出此ComboBox时,另一个ComboBox应该填充该特定美国州内的县列表。但是,当光标移出ComboBox时,会产生错误,但是,美国县的ComboBox确实有值,这没有任何意义。我不确定为什么会产生错误。

这是我的代码。

CREATE PROCEDURE GetStateCounties
(
    @TheState AS CHAR(2)
)
AS
BEGIN
    SELECT S.StateID AS SPK,
           S.StateAbbrev AS SA,
           SC.StateID AS SFK,
           SC.CountyName AS CN
    FROM States AS S
    INNER JOIN StatesCounties AS SC
    ON S.StateID = SC.StateID
    WHERE S.StateAbbrev = @TheState
    ORDER BY CN
END

填充ComboBox的VB代码是(Function):

Public Function PopulateCounties(ByVal TheState As String) As List(Of String)

    Dim LoadCounties As New List(Of String)
    Dim dr As SqlDataReader

    Using SetDatabaseConnection As SqlConnection = New SqlConnection(ConnectToDatabase)
        Using cmd As SqlCommand = New SqlCommand
            Try
                With cmd
                    .CommandText = "GetStateCounties"
                    .CommandType = CommandType.StoredProcedure
                    .Connection = SetDatabaseConnection
                    .Parameters.Add("@TheState", SqlDbType.Char).Value = TheState
                End With

                With SetDatabaseConnection
                    If .State = ConnectionState.Broken _
                    OrElse .State = ConnectionState.Closed Then
                        .Open()
                    End If
                End With

                dr = cmd.ExecuteReader

                While dr.Read
                    LoadCounties.Add(dr("CN"))
                End While
            Catch RangeEx As IndexOutOfRangeException
                MessageBox.Show(RangeEx.ToString(), _
                                "Index Out Of Range Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch CastEx As InvalidCastException
                MessageBox.Show(CastEx.ToString(), _
                                "Invalid Cast Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch ArgNullEx As ArgumentNullException
                MessageBox.Show(ArgNullEx.ToString(), _
                                "Argument Null Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch ArgEx As ArgumentException
                MessageBox.Show(ArgEx.ToString(), _
                                "Argument Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch SQLEx As SqlException
                MessageBox.Show(SQLEx.ToString(), _
                                "SQL Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch InvalidOpEx As InvalidOperationException
                MessageBox.Show(InvalidOpEx.ToString(), _
                                "Invalid Operation Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch NotSuppEx As NotSupportedException
                MessageBox.Show(NotSuppEx.ToString(), _
                                "Not Supported Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch NullRefEx As NullReferenceException
                MessageBox.Show(NullRefEx.ToString(), _
                                "Null Reference Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Finally
                With SetDatabaseConnection
                    If .State = ConnectionState.Open Then
                        .Close()
                    End If
                End With
            End Try
        End Using
    End Using

    Return LoadCounties
End Function

Private Sub ComboBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles State.LostFocus, AddressType.LostFocus, PhoneNumberType.LostFocus, EmailAddressType.LostFocus
    Try
        With CType(sender, ComboBox)
            .BackColor = Color.White
            .ForeColor = Color.Black

            If .Name.Equals("State") Then
                Dim TheCountiesToAdd = PopulateCounties(State.Text.ToString.Trim)

                For Each TheCounties As String In TheCountiesToAdd
                    With CType(Territory, ComboBox)
                        .Items.Clear()
                        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                        .AutoCompleteSource = AutoCompleteSource.ListItems
                        .DataSource = TheCountiesToAdd
                        .DisplayMember = "CN"
                        .ValueMember = "CN"
                    End With
                Next TheCounties
            End If
        End With
    Catch CastEx As InvalidCastException
        MessageBox.Show(CastEx.ToString(), _
                        "Invalid Cast Exception", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error, _
                        MessageBoxDefaultButton.Button1)
    Catch OutOfMemEx As OutOfMemoryException
        MessageBox.Show(OutOfMemEx.ToString(), _
                        "Out Of Memory Exception", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error, _
                        MessageBoxDefaultButton.Button1)
    Catch ArgEx As ArgumentException
        MessageBox.Show(ArgEx.ToString(), _
                        "Argument Exception", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error, _
                        MessageBoxDefaultButton.Button1)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

改变这个:

For Each TheCounties As String In TheCountiesToAdd                
    With CType(Territory, ComboBox)
        .Items.Clear()
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
        .DataSource = TheCountiesToAdd
        .DisplayMember = "CN"
        .ValueMember = "CN"
    End With
Next TheCounties

由:

With CType(Territory, ComboBox)
     .Items.Clear()
     .AutoCompleteMode = AutoCompleteMode.SuggestAppend
     .AutoCompleteSource = AutoCompleteSource.ListItems
     .DataSource = ConvertToDataTable(TheCountiesToAdd)
     .DisplayMember = "CN"
     .ValueMember = "CN"
End With

这里的功能:

Public Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable
    Dim table As New DataTable()
    table.Columns.Add("CN")
    For Each item As T In list
        Dim row As DataRow = table.NewRow()
        row("CN") = item
        table.Rows.Add(row)
    Next
    Return table
End Function