This is a migrated thread and some comments may be shown as answers.

Binding Combobox to items and setting SelectedValue to field value?

1 Answer 108 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
sbowman
Top achievements
Rank 1
sbowman asked on 10 Jan 2009, 07:47 PM
I have a web form with a bunch of telerik combo boxes on it that need to be populated with an array of numbers and then the selecteditem needs to be set equal to a db item. The problem I am having is that when I set the selected item equal to a db item, it replaces the first item in the array.

aspx code:

<

td ><telerik:RadComboBox ID="rcboNumAdultsFL" runat="server" Width="45px" BorderColor="#999999"></telerik:RadComboBox></td>

VB.NET code:

 

Private

Sub BindToNumberList(ByVal combo As RadComboBox)

 

 

Dim itemsList As String() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}

 

combo.DataSource = itemsList

combo.DataBind()

 

End Sub

 

Protected

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

 

 

If

Not Page.IsPostBack Then

 

BindToNumberList(

Me.rcboNumAdultsFL)

 

'Populate the datatable

 

 

 

Dim sConnString As String = ConfigurationManager.ConnectionStrings("PU89").ConnectionString

 

 

Dim objConnection As New SqlClient.SqlConnection(sConnString)

 

 

Dim strSQL As String = "SELECT * FROM tblRegistration WHERE ID='" & _ID & "';"

 

 

 

 

Dim dbComm As New SqlClient.SqlCommand(strSQL, objConnection)

 

 

Dim objDR As SqlClient.SqlDataReader

 

 

Try

 

 

 

objConnection.Open()

objDR = dbComm.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

 

While objDR.Read()

 

 

With objDR

 

Me

.rcboNumAdultsFL.SelectedItem.Value = .Item("NumAdultsFL")

 

 

Me.rcboNumAdultsFL.SelectedItem.Text = .Item("NumAdultsFL")

 

End

With

 

 

 

 

End While

 

 

 

 

Catch ex As Exception

 

Response.Write(ex.Message)

Response.End()

 

Finally

 

 

 

 

If objConnection.State = ConnectionState.Open Then

 

 

 

objConnection.Close()

 

 

 

 

End If

 

 

 

 

End Try

 

 

 

 

End If
End Sub

This code results in the combo box showing the correct value from the db but it replaces the "0" in the array list. How do I get it to work properly?

Thanks,
Shelley

 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Jan 2009, 10:43 AM
Hi Sbowman,

I hope you want to set the RadComboBoxItem after reading the value from database. If that the case, please use Me.rcboNumAdultsFL.SelectedValue instead of Me.rcboNumAdultsFL.SelectedItem.Value and Me.rcboNumAdultsFL.SelectedItem.Text.
Working with Items in Server Side Code

Thanks,
Shinu.
Tags
ComboBox
Asked by
sbowman
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or