Click here to Skip to main content
12,063,346 members (73,105 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET
<asp:RadioButtonList ID="Rad" runat="server"
                        onselectedindexchanged="RadioButtonList2_SelectedIndexChanged"
                        RepeatDirection="Horizontal">
                        <asp:ListItem Value="0">c</asp:ListItem>
                        <asp:ListItem Value="1">a</asp:ListItem>
                        <asp:ListItem Value="0">c</asp:ListItem>
                    </asp:RadioButtonList>
c# code:
protected void  RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;
    if (rb != null)
    {
        if (rb.Checked)
        {
 
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand("insert into rd_table(radio) values('" + Rad.SelectedValue + "')", sqlcon);
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
        }
    }
 
}
It does not insert in sqlserver,,plz modified these code
Posted 11-Apr-13 20:15pm
Edited 11-Apr-13 20:37pm
v2
Comments
PrashantSonewane 12-Apr-13 1:23am
   
Are you getting any error?
Aarti Meswania 12-Apr-13 1:34am
   
unclear question
what is exact problem?
Member 9959371 12-Apr-13 1:36am
   
it does not insert value in server(database),,plz modified
Aarti Meswania 12-Apr-13 1:39am
   
it depends...
1st connection is opened successfully?
2nd check query it should not give error, and if it is throwing error then mention that error then we can help you :)
rmksiva 12-Apr-13 1:41am
   
Can you mention DB Datatype for "radio" Field in Insert Query IF Your DB Type is MS SQL SEVER and Datatype of "radio" is bit ! Then Please check Below solution !
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

C# Code

protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection sqlcon = new SqlConnection("Your Connection String Here");
            for (int i = 0; i < Rad.Items.Count; i++)
            {
                    if (Rad.Items[i] != null && Rad.Items[i].Selected)
                    {
                        sqlcon.Open();
                        SqlCommand sqlcom = new SqlCommand("insert into rd_table(radio) values('" + Rad.SelectedValue + "')", sqlcon);
                        sqlcom.ExecuteNonQuery();
                        sqlcon.Close();
                        break;
                    }
               }
        }


HTML Code

<asp:RadioButtonList ID="Rad" runat="server" OnSelectedIndexChanged="RadioButtonList2_SelectedIndexChanged"
       RepeatDirection="Horizontal" AutoPostBack="true">
       <asp:ListItem Value="0">c</asp:ListItem>
       <asp:ListItem Value="1">a</asp:ListItem>
       <asp:ListItem Value="0">c</asp:ListItem>
   </asp:RadioButtonList>


Thanks,
Prashant
Mark Solution if this work for you!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

sqlcon.Open();
 
SqlCommand sqlcom = new SqlCommand("insert into rd_table(radio) values(" + ((Rad.SelectedValue) ? 1 :0)  + ")", sqlcon);
 
sqlcom.ExecuteNonQuery();
 
sqlcon.Close();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160204.4 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100