Insert selected radio button values to database

Satyapriyanayak
Posted by Satyapriyanayak under C# category on | Points: 40 | Views : 23009
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_radiobutton_value_windows
{
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;
}
}
}

Comments or Responses

Login to post response