You must Sign In to post a response.

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


Comments

Author: Raghavendra24 Jan 2012 Member Level: Silver   Points : 3

Hi Declare radiobuttonlist as below






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;
}

Author: Ravindran24 Jan 2012 Member Level: Diamond   Points : 2

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)

Author: niraj kumar mundel24 Jan 2012 Member Level: Silver   Points : 4

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 description for connection
///

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

Author: Pawan Awasthi24 Jan 2012 Member Level: Gold   Points : 2

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.

Author: Vijayalakshmi G M24 Jan 2012 Member Level: Gold   Points : 2

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.

Author: Naveen Reddy24 Jan 2012 Member Level: Gold   Points : 2

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

Author: sugandha24 Jan 2012 Member Level: Gold   Points : 2

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.

Author: SonyShiva25 Jan 2012 Member Level: Gold   Points : 2

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

Author: Kamlesh Mohanty25 Jan 2012 Member Level: Gold   Points : 1

Hi!
>>Try the given below code in code behind


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


Sign In to post your comments