Wednesday, 17 August 2016

Save (Insert) RadioButton value to SQL Server Database in ASP.Net using C#

Save (Insert) RadioButton value to SQL Server Database in ASP.Net using C#
Description:

in this example we explain that how to Save or Insert RadioButton value to Database in asp.net using C#.or how to insert RadiButton value into SQL Server database using C#.

as we know that generally we store the  RadioButton  value instead of text.because the text may be the changed so in future you have to just changed in design side not in backed or in SQL Server.
So how to access the value of RadioButton and save it into SQL Server Database.

Below is the example that demonstrate the store the Gender value in database.

SaveRadioButtonValue.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SaveRadioButtonValue.aspx.cs"
    Inherits="SaveRadioButtonValue" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Save (Insert) RadioButton value to Database in ASP.Net using C# and VB.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                First Name:
            </td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                Last Name:
            </td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                Gender:
            </td>
            <td>
                <asp:RadioButton ID="rdbMale" Text="Male" runat="server" GroupName="Gender" />
                <asp:RadioButton ID="rdbFemale" Text="Female" runat="server" GroupName="Gender" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

 SaveRadioButtonValue.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

public partial class SaveRadioButtonValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string FirstName = txtFirstName.Text.Trim();
            string LastName = txtLastName.Text.Trim();
            string gender = string.Empty;
            //Here we store 0 for Male and 1 for Female in database
            if (rdbMale.Checked)
            {
                gender = "0";
            }
            else if (rdbFemale.Checked)
            {
                gender = "1";
            }
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Gender(FirstName,LastName,Gender) VALUES(@FirstName,@lastName,@Gender)"))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@FirstName", FirstName);
                    cmd.Parameters.AddWithValue("@LastName", LastName);
                    cmd.Parameters.AddWithValue("@Gender", gender);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}


0 comments:

Post a Comment