With a group of radio buttons, you can first find out what radio button the user selected, and then pass the caption of that radio button to the column of the table, provided the caption of the radio button holds the exact same text the column is supposed to receive. Here is an example:
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 StringDataEntry1
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnAddEmployee_Click(object sender, EventArgs e)
{
string strMaritalStatus = "";
if (rdoMarried.Checked == true)
strMaritalStatus = rdoMarried.Text;
else if (rdoSeparated.Checked == true)
strMaritalStatus = rdoSeparated.Text;
else if (rdoDivorced.Checked == true)
strMaritalStatus = rdoDivorced.Text;
else if (rdoWidow.Checked == true)
strMaritalStatus = rdoWidow.Text;
else
strMaritalStatus = rdoSingle.Text;
using (SqlConnection cntExercise =
new SqlConnection("Data Source='EXPRESSION';" +
"Database='Exercise1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise =
new SqlCommand("INSERT INTO Personnel.Employees(EmployeeName, " +
"MaritalStatus) VALUES(N'" + txtEmployeeName.Text + "', N'" +
strMaritalStatus + "');",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
}
As an alternative to a combo box, you use a control that presents two choices as Yes/No or True/False to the user. When the user has made a selection, you can find out what the user selected, give it a value of 1 or 0, and then pass it as a numeric value to the column. In this case, or whenever you pass the value as a number, you can omit the single-quotes. Here is an example:
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 Exercise2
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
}
private void btnAddEmployee_Click(object sender, EventArgs e)
{
int iHasChildren = 0;
int iMaritalStatus = 0;
if (rdoMarried.Checked == true)
iMaritalStatus = 1;
else if (rdoSeparated.Checked == true)
iMaritalStatus = 2;
else if (rdoDivorced.Checked == true)
iMaritalStatus = 3;
else if (rdoWidow.Checked == true)
iMaritalStatus = 4;
else
iMaritalStatus = 0;
if (rdoYes.Checked == true)
iHasChildren = 1;
else
iHasChildren = 0;
using (SqlConnection cntExercise =
new SqlConnection("Data Source='EXPRESSION';" +
"Database='Exercise1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise =
new SqlCommand("INSERT INTO Personnel.Employees " +
"VALUES('" + txtEmployeeName.Text + "', " +
iMaritalStatus + ", " + iHasChildren + ");",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
Radio buttons work as a mutually-exclusive group where the user can select one item at a time. Although the functionality of radio buttons presents various options (for example some implementations treat each value of a radio button as an integer), a radio button primarily holds text. As such, it can follow the same rules as a label. The issue is that radio buttons behave in a group. This means that you must know what radio button in the group you are referring to. To do this, you can use a conditional statement. Here are examples: 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 Exercise2
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnBinder_Click(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise =
new SqlCommand("SELECT * FROM Personnel.Employees;",
cntExercise);
cntExercise.Open();
SqlDataReader rdrExercise = cmdExercise.ExecuteReader();
while(rdrExercise.Read())
{
txtEmployeeName.Text = rdrExercise[3].ToString();
if (rdrExercise[5].ToString() == "Full Time")
rdoFullTime.Checked = true;
else if (rdrExercise[5].ToString() == "Part Time")
rdoPartTime.Checked = true;
else
rdoUnspecified.Checked = true;
}
}
}
}
}
|
||||||