You must Sign In to post a response.
  • Category: ASP.NET

    How can i store the value of radio button in sql server

    hi..friends
    how can i store the value of radio button in sql server
    such as i have a field named gender in registration form
    how can i get the of this control in my database table
    Thank
    sunil
  • #653452
    Hi Declare radiobuttonlist as below
    <asp:RadioButtonList ID="RDS" runat="server" >
    <asp:ListItem Text="Male" Value="1"></asp:ListItem>
    <asp:ListItem Text="Female" Value="2"></asp:ListItem>
    </asp:RadioButtonList>


    Store the value of the gender in table in DB

    Then set the value of the radiobuttonlist as

    if(table[Gender]=="Male")
    {
    RDS.Items[0].Selected = true;
    }
    else
    RDS.Items[1].Selected = true;
    }

  • #653456
    sunil pareek

    Just assign it in one variable like this


    if (radbtnMale.Checked==true)
    {
    sex=Male;
    }
    else if (radbtnFeMale.Checked==true)
    {
    sex=Female;
    }

    //write query like this
    insert into tblname(sex) values(sex)

    Regards
    N.Ravindran
    Your Hard work never fails

  • #653463
    hi dear first of all define a class

    using this code


    using System;
    using System.Data;
    using System.Configuration;
    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;
    using System.Data.SqlClient;
    /// <summary>
    /// Summary description for connection
    /// </summary>
    public class connection
    {
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr;


    public connection()
    {
    //
    // TODO: Add constructor logic here
    //
    }

    public SqlDataReader fillgrid(string s)
    {
    if (con.State == ConnectionState.Open)
    {
    con.Close();
    }
    con.Open();
    cmd.CommandText = s;
    cmd.Connection = con;
    dr = cmd.ExecuteReader();

    return dr;
    con.Close();


    }

    public void nonExec(string s)
    {
    if (con.State == ConnectionState.Open)
    {
    con.Close();
    }
    con.Open();
    cmd.CommandText = s;
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    con.Close();
    }
    public SqlDataReader loginExec(string s)
    {
    if (con.State == ConnectionState.Open)
    {
    con.Close();
    }
    con.Open();
    cmd.CommandText = s;
    cmd.Connection = con;
    dr = cmd.ExecuteReader();
    return dr;
    con.Close();
    }
    }



    then

    use

    this code

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
    //if (txt_Password.Text.ToString() != txt_ConfirmPassword.Text.ToString())
    //{
    // lbl_chkpsw.Text = "Please Correct Password";
    // lbl_chkpsw.ForeColor = System.Drawing.Color.Red;
    //}


    if (flag == true)
    {

    lbl_Avail.Visible = true;
    lbl_Avail.Text = "cannot submit data because user is already registered";
    }
    else
    {

    string s = "insert into info(guid,fname,lname,username,password,cpassword,email,dob,gender,phone,country,state,city,zip,address,specification) values('" + g + "','" + txt_FirstName.Text + "','" + txt_LastName.Text + "','" + txt_UserName.Text + "','" + txt_Password.Text + "','" + txt_ConfirmPassword.Text + "','" + txt_Email.Text + "','" + ddl_MM.Text + "/" + ddl_DD.Text + "/" + ddl_YY.Text + "','" + rbl_Gen.Text + "'," + txt_Phone.Text + ",'" + ddl_Country.Text + "','" + ddl_State.Text + "','" + txt_City.Text + "'," + txt_Zip.Text + ",'" + txt_Address.Text + "','" + txt_Specification.Text + "')";
    c.nonExec(s);

    //SqlCommand cmd = new SqlCommand(s, con);

    //con.Open();
    //cmd.ExecuteNonQuery();
    //lbl_Msg.Visible = true;

    // lbl_Msg.Text = "Data Inserted Succesfully";
    // Response.Redirect("Welcome.aspx");
    //con.Close();
    string sql = "select *from info";
    GridView1.DataSource = c.fillgrid(sql);
    GridView1.DataBind();



    may this code help you

    Regards
    NiraJ kumar Mundel

  • #653480
    Hai Sunil,

    there are various ways to keep the radio button value into your database:-

    1. Keep directly the value of the radio button
    2. Keep the flag 'true' or 'false' in the database if you have only 2 options.
    3. Keep only a single character like 'M' or 'F' in the database.

    So you can use any of these ways to implement it.
    The most efficient is keeping only a single character to reduce the memory of your database. it needs only a single character.

    But in this, while retrieving , you need to write the code accordingly. Like if the returned value is 'M', the selected male else Female.

    Hope it will give you enough idea.
    Let me know if you need the implementation details or if you stuck anywhere while implementation.
    Hope it will help.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #653485
    Hi,

    You can store the Value in two ways.

    If only two Radio buttons are there store the value as Boolean.

    If more than two Radio button are there store the value as string.

    Vijayalakshmi G M
    IT Professional Trainer
    www.wincorporatetraining.com

  • #653491
    Hello,

    You can check the radiobutton check like as below


    if (rbMale.Checked==true)
    {
    sex=M;
    }
    else if (rbFeMale.Checked==true)
    {
    sex=F;
    }




    now write the code to insert these selected value into database

    Regards,
    Naveen

  • #653504
    Try below code:

    string gender = null;
    if (rbtn.Checked==true)
    {
    gender=Male;
    }
    else if (rbtn.Checked==true)
    {
    gender=Female;
    }



    Then you can use gender variable to insert the value in database column.

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..

  • #653615
    Hi sunil,


    Yes,
    In Table one field which is varchar.
    If male radio buton is checked, assign 'male' to that variable
    IF female radio buton is checked, assign 'female' to that variable.

    Similarly, while retrieving,
    if male, you check the male radio buton n vice versa

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #653695
    Hi!
    >>Try the given below code in code behind


    if (Page.IsPostBack)
    {
    Message.Text = "You have selected the " + radio1.SelectedItem.Value;
    }
    [/code

    Regards
    Kamlesh Mohanty


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.