Previous..
Next..
Here i will explain that how to use checkBox and Redio Button in window form when we insert data into database.
Step(1):Form
Drag and down two redio button for male and female. You can see my tutorial 6 that how to insert data into database.
Step(2):Code
Coding is same as tutorial 6 only difference is for redio button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace First_Csharp_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String gender; //we have to define this
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(str);
String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
String query = "insert into data (E.id,name,surname,age,gender) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"' , '"+this.gender+"')";
SqlCommand cmd = new sqComamnd(query,con);
SqlDataReader dbr;
SqlDataReader dbr;
try
{
con.open();
dbr = cmd.ExecuteReader();
MessageBox.Show("saved");
while(dbr.read())
{
}
MessageBox.Show("saved");
while(dbr.read())
{
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "male";
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "female";
}
}
}
//You can do this for check box.
Good tutorials.....
ReplyDelete