How to save the value of radiobuttonlist in database asp.net C#


Data Base(Sql Server)

USE [radiobutton]
GO

/****** Object:  Table [dbo].[radiotable]    Script Date: 03/22/2014 12:42:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[radiotable](
      [value] [nvarchar](max) NULL
) ON [PRIMARY]

GO




Source code

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <strong> Which mobile phone do you like the most ? </strong><asp:RadioButtonList
            ID="RadioButtonList1" runat="server" AutoPostBack="True"
            onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
            <asp:ListItem>Samsung</asp:ListItem>
            <asp:ListItem>Mobile</asp:ListItem>
            <asp:ListItem>LG</asp:ListItem>
            <asp:ListItem>Lava</asp:ListItem>
        </asp:RadioButtonList>
   
    </div>
    </form>
</body>
</html>


C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;

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

    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC; Initial Catalog =radiobutton ;User ID=sa; Password=12345678");


            con.Open();
            SqlCommand cmd = new SqlCommand("insert into radiotable (value)values('" + RadioButtonList1.Text + "')", con);
            cmd.ExecuteNonQuery();
            lbresult.Text = "your data suessfully saved";
            lbresult.ForeColor = Color.Green;
        }

        catch
        {
            lbresult.Text = "your data is not saved";
            lbresult.ForeColor = Color.Red;
        }
    }
}


Out-put:-



How to save the value of radiobuttonlist in database asp.net C# How to save the value of radiobuttonlist in database asp.net C# Reviewed by NEERAJ SRIVASTAVA on 2:23:00 PM Rating: 5

No comments:

Powered by Blogger.