Tuesday, 10 January 2012

How to save The RadioButtonlist values in asp.net

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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <h2 style=" color:blue; font-style:normal ">  
            How to save RadioButtonlist 
            <br />values in asp.net  
        </h2>  
    <div>
       <asp:RadioButtonList ID="RadioButtonList1" runat="server">
       <asp:ListItem>Male</asp:ListItem>
       <asp:ListItem>Female</asp:ListItem>
    </asp:RadioButtonList>
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label><br />
    
        <asp:Button ID="Button1" runat="server" Text="SAVE" OnClick="btnsave" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

    }
    protected void btnsave(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue != "")
        {

            string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into [Gender]values(@gender)";
            cmd.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedValue);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = RadioButtonList1.SelectedValue +" was saved sucessesfully";
            sqlcon.Close();
        }
        else
        {
            Label1.Text = "please check gender";
        }


    }

}




No comments:

Post a Comment