从VB / sql server填充ComboBox

时间:2014-04-25 19:01:18

标签: asp.net sql-server vb.net combobox

我有一个存储过程AutoDeliveryReportsByUser,它根据参数@Logins返回结果。我尝试在radcombobox中填充db列Emails的所有结果。我能够抓住列的最后一个值并将其放入标签中(这是不好的)但是却无法在组合框中获得任何内容。

Protected Sub ReportEmailList()
            'sUser = Mid(HttpContext.Current.User.Identity.Name, InStr(HttpContext.Current.User.Identity.Name, "\", CompareMethod.Text) + 1)
            sUser = "athens1"
            Dim cmd As New SqlCommand("AutoDeliverReportsByUser", GetUDBSQLConn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@Logins", SqlDbType.VarChar)
            cmd.Parameters("@Logins").Value = sUser
            cmd.Connection.Open()

            Dim r As SqlDataReader = cmd.ExecuteReader
            While r.Read
                LblEmailList.Text = r("Emails")
                RadCBEmailList.DataTextField = r("Emails")

            End While

            cmd.Connection.Close()
            cmd.Dispose()

        End Sub

1 个答案:

答案 0 :(得分:0)

我认为你还没有完全理解如何将控件绑定到数据

当您使用阅读器以这种方式阅读时,它会从第一个到最后一个按记录读取记录。第二点不是绑定控件的正确方法。以下是您重新访问的代码:

  Protected Sub ReportEmailList()
    'Create a new dataadapter and dataset which will be filled from datadapter.
    Dim Da As New System.Data.SqlClient.SqlDataAdapter
    Dim Ds As New System.Data.DataSet
    Dim sUser = "athens1"
    Dim cmd As New System.Data.SqlClient.SqlCommand("AutoDeliverReportsByUser", GetUDBSQLConn)
    cmd.CommandType = Data.CommandType.StoredProcedure
    cmd.Parameters.Add("@Logins", SqlDbType.VarChar)
    cmd.Parameters("@Logins").Value = sUser
    cmd.Connection.Open()
    'Associate select command to dataadpter and then fill the databset
    'now all your data are within dataset(DS) and you can access to each table where there's more than one with its name or by index 0 base as per example below
    Da.SelectCommand = cmd
    Da.Fill(Ds)

    With RadCBEmailList
        'Bind source data to rad component datasource
        .DataSource = Ds.Tables(0)
        'assgin to the textfield the  column name needed
        .DataTextField = "Emails"
        'assing to the value field the column needed in example ID or any other one.
        .DataValueField = "Emails"
        'Call bind method to complete
        .DataBind()
        'Extra if you want to add a specific item to invite the customer to make something from the combo box not sure if it work with rad control it works fine with simple dropdownlist
        '.Items.Insert(0, "Choose an email") it will be the first item into your control according previous comment
    End With

    End Sub

如果符合您的要求,请标记为已回答