2014-04-25 21 views
0

我有一個存儲過程AutoDeliveryReportsByUser,它根據其參數@Logins返回結果。我試圖在radcombobox中填充db列Emails的所有結果。我能夠抓住列的最後一個值並將其放在一個標籤中(這是不好的),但沒有能夠在組合框中獲取任何東西。從VB/sql服務器填充組合框

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 

回答

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 

,如果它符合您的要求,請標記爲回答