How to insert selected radio button values while registration . [Resolved]

Posted by Srinibaschampati under C# on 8/13/2013 | Points: 10 | Views : 2342 | Status : [Member] | Replies : 2
First of all it is a windows app.Here I have 3 textboxes name, address ,age and a group box contains two radio buttons as male and female respectively.There is a register button .




Responses

Posted by: Satyapriyanayak on: 8/13/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Try like this.

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Insert
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"server = HOME-SAT/SQLEXPRESS;data source = .; initial catalog = test; user id=sa;pwd =pintu ;integrated security = SSPI");
SqlCommand com;

public Form1()
{
InitializeComponent();
}

private void btn_insert_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
MessageBox.Show("Please enter name");
txtName.Focus();
}
else if (rbtn_male.Checked == false && rbtn_female.Checked == false)
{
MessageBox.Show("Please select radiobutton");
}

else if (rbtn_male.Checked == true)
{
com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into register values(@Name,@Gender)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@Name", txtName.Text);
com.Parameters.AddWithValue("@Gender", rbtn_male.Text);
if (con.State == ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data entered successfully!!!");
clear();
}
else
{
com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into register values(@Name,@Gender)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@Name", txtName.Text);
com.Parameters.AddWithValue("@gender", rbtn_female.Text);
if (con.State == ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data entered successfully!!!");
clear();
}
}
void clear()
{
txtName.Text = "";
rbtn_male.Checked = false;
rbtn_female.Checked = false;
}
}
}


If this post helps you mark it as answer
Thanks

Srinibaschampati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/14/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample example without any validations......

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RadioButtonEx
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"data source = .; initial catalog = DBName ; user id=testLogin ;password =pwd@1234 ");
SqlCommand com;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void btnRegister_Click(object sender, EventArgs e)
{
com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into WindowRadioButton values(@Name, @Address, @Age, @Gender)";
com.Parameters.Clear();

int gender = (rbtnMale.Checked == true) ? 1 : 0; // This is for storing gender value into data base... 1= Male , 0 = Female
com.Parameters.AddWithValue("@Name", txtName.Text);
com.Parameters.AddWithValue("@Address", txtName.Text);
com.Parameters.AddWithValue("@Age", txtAge.Text);
com.Parameters.AddWithValue("@gender", gender);
if (con.State == ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data entered successfully!!!");
clear();
}

private void clear()
{
txtName.Text = "";
txtAddress.Text = "";
txtAge.Text = "";
}
}
}


Controls are:
3 Text Boxes: txtName, txtAddress, txtAge,
2 Radio Buttons: rbtnFemale, rbtnMale
1 Button : btnRegister
Table Structure:
CREATE TABLE WindowRadioButton 

( Name VARCHAR(20),
Address VARCHAR(50),
Age INT,
Gender INT)


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Srinibaschampati, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response