Multiple Selection of radcombobox

2 Answers 3301 Views
ComboBox
Edd
Top achievements
Rank 1
Edd asked on 14 Nov 2013, 07:57 AM
Good day,
please am using a radcombobox that allows for multiple selection.
My problem is how can i insert the selected items into a database one by one.
Can someone please help me
thank you

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Nov 2013, 09:08 AM
Hi Edd,

Please have a look into the code snippet to insert the Selected Item of RadComboBox into DataBase on RadButton OnClick event.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" CheckBoxes="true">
    <Items>
        <telerik:RadComboBoxItem Text="Australia" runat="server" />
        <telerik:RadComboBoxItem Text="Austria" runat="server" />
        <telerik:RadComboBoxItem Text="Bahrain" runat="server" />
        <telerik:RadComboBoxItem Text="Bangladesh" runat="server" />
        <telerik:RadComboBoxItem Text="Belgium" runat="server" />
        <telerik:RadComboBoxItem Text="Bhutan" runat="server" />
    </Items>
</telerik:RadComboBox>
<telerik:RadButton ID="RadButton1" runat="server" Text="Get Selected Item" OnClick="RadButton1_Click">
</telerik:RadButton>

C#:
protected void RadButton1_Click(object sender, EventArgs e)
{
    foreach (RadComboBoxItem item in RadComboBox1.Items)
    {
        if (item.Checked == true)
        {
            String connstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);
            conn.Open();
            SqlCommand cmd = new SqlCommand("insert into CountryTable (CountryName) values('" + item.Text + "')", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
    Response.Write("<script>alert('Inserted')</script>");
}

Thanks,
Shinu.
Luis
Top achievements
Rank 1
Veteran
commented on 14 Jun 2020, 09:24 AM

And how can I get the items from a column in a SQL Server table? 

Luis

0
Doncho
Telerik team
answered on 17 Jun 2020, 04:23 PM

Hi Luis,

You can check out the different DataSources that can be used for binding data to the RadComboBox in the Data Binding Overview | RadComboBox article.

Note that the RadComboBox uses a DataSource and has no direct relation to the SQL database itself.

Here is a sample of binding a single column DataTable to the RadComboBox

VB code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim connstring As String = WebConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connstring)
        Dim myCommand As SqlDataAdapter = New SqlDataAdapter("SELECT DISTINCT [Country] FROM [Customers]", conn)
        
Dim dt As DataTable = New DataTable() myCommand.Fill(dt)
RadComboBox1.DataSource = dt RadComboBox1.DataTextField = "Country" RadComboBox1.DataValueField = "Country" RadComboBox1.DataBind() End If

I hope this will prove helpful!

Kind regards,
Doncho
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
ComboBox
Asked by
Edd
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Doncho
Telerik team
Share this question
or