In this article I will explain how to save (insert) RadioButton value to SQL Server database in ASP.Net using C# and VB.Net.
The value of the checked RadioButton will be saved to SQL Server database on Button click in ASP.Net.
 
Database
I have made use of the following table Persons with the schema as follows.
Save (Insert) RadioButton value to Database in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of a TextBox, two RadioButtons and a Button.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            Name:
        </td>
        <td>
            <asp:TextBox ID="txtName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Gender:
        </td>
        <td>
            <asp:RadioButton ID="rbMale" Text="Male" runat="server" GroupName="Gender" />
            <asp:RadioButton ID="rbFemale" Text="Female" runat="server" GroupName="Gender" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button Text="Submit" runat="server" OnClick="Submit" />
        </td>
    </tr>
</table>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Save (Insert) RadioButton value to Database in ASP.Net
Inside the Submit Button click event handler, the value of Name TextBox is fetched and saved in a variable and then based on whether the Male or Female CheckBox is checked the value of Gender variable is set as M or F respectively.
Finally the Name and Gender values are inserted into the SQL Server Database table.
C#
protected void Submit(object sender, EventArgs e)
{
    string name = txtName.Text.Trim();
    string gender = string.Empty;
    if (rbMale.Checked)
    {
        gender = "M";
    }
    else if (rbFemale.Checked)
    {
        gender = "F";
    }
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Persons(Name, Gender) VALUES(@Name, @Gender)"))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Gender", gender);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
 
VB.Net
Protected Sub Submit(sender As Object, e As EventArgs)
    Dim name As String = txtName.Text.Trim()
    Dim gender As String = String.Empty
    If rbMale.Checked Then
        gender = "M"
    ElseIf rbFemale.Checked Then
        gender = "F"
    End If
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("INSERT INTO Persons(Name, Gender) VALUES(@Name, @Gender)")
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@Name", name)
            cmd.Parameters.AddWithValue("@Gender", gender)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using
End Sub
 
 
Screenshots
Page with TextBox and RadioButtons
Save (Insert) RadioButton value to Database in ASP.Net using C# and VB.Net
 
The values saved in database table
Save (Insert) RadioButton value to Database in ASP.Net using C# and VB.Net
 
 
Downloads

Download Code