You are on page 1of 19

C# WINS APPLICATION SAMPLES

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

namespace WindowsFormsApp22
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"server=DESKTOP-
35Q7P1P\SQLEXPRESS;trusted_connection=yes;database=librarys");
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd3 = new SqlCommand("select count(*)+1 from student1", con);
SqlDataReader dr3;
dr3 = cmd3.ExecuteReader();
while (dr3.Read())
{
textBox1.Text = dr3.GetValue(0).ToString();
}
dr3.Close();

SqlCommand cmd45 = new SqlCommand("select name from student1", con);


SqlDataReader dr45;
dr45 = cmd45.ExecuteReader();

1
C# WINS APPLICATION SAMPLES
while (dr45.Read())
{
comboBox4.Items.Add(dr45.GetValue(0).ToString());
}
dr45.Close();
con.Close();
}

private void button1_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd3 = new SqlCommand("select count(*)+1 from student1", con);
SqlDataReader dr3;
dr3 = cmd3.ExecuteReader();
while (dr3.Read())
{
textBox1.Text = dr3.GetValue(0).ToString();
}
dr3.Close();
con.Close();
clear();
}
public void clear()
{

textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";

comboBox1.Text = "";
comboBox2.Text = "";
comboBox5.Text = "";

richTextBox1.Text = "";

}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into student1 values(" +
textBox1.Text + ",'" + textBox3.Text + "','" + comboBox1.SelectedItem.ToString() +
"','" + textBox4.Text + "','" + comboBox2.SelectedItem.ToString() + "','" +
comboBox5.SelectedItem.ToString() + "','" + richTextBox1.Text + "','" + textBox5.Text
+ "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("saved successfully");
con.Close();
}

private void button9_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("select * from student1", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "student");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "student";
con.Close();
}

2
C# WINS APPLICATION SAMPLES
private void button6_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd9 = new SqlCommand("select * from student1 where name = '" +
comboBox4.SelectedItem.ToString() + "'", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd9);
DataSet ds = new DataSet();
ad.Fill(ds, "student");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "student";
con.Close();
}

private void button5_Click(object sender, EventArgs e)


{
comboBox4.Items.Clear();
con.Open();
SqlCommand cmd45 = new SqlCommand("select name from student1", con);
SqlDataReader dr45;
dr45 = cmd45.ExecuteReader();
while (dr45.Read())
{
comboBox4.Items.Add(dr45.GetValue(0).ToString());
}
dr45.Close();
con.Close();

private void button4_Click(object sender, EventArgs e)


{
if (MessageBox.Show("do you want to delete", "delete",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
con.Open();
SqlCommand cmd8 = new SqlCommand("delete student1 where studentid='" +
dataGridView1.SelectedCells[0].Value + "'", con);
cmd8.ExecuteNonQuery();
con.Close();
con.Open();
SqlCommand cmd = new SqlCommand("select * from student1", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "student");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "student";
con.Close();
}

private void button3_Click(object sender, EventArgs e)


{
this.Close();
}
}
}

3
C# WINS APPLICATION SAMPLES

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

4
C# WINS APPLICATION SAMPLES
using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsApplication1

public partial class frmbuild : Form

SqlConnection con = new SqlConnection("server=(local); database=cms ;


integrated security = true");

public frmbuild()

InitializeComponent();

txtname.KeyPress += new KeyPressEventHandler(txtname_KeyPress);

txtaddress.KeyPress += new KeyPressEventHandler(txtaddress_KeyPress);

cmbstatus.KeyPress += new KeyPressEventHandler(cmbstatus_KeyPress);

txtdos.KeyPress += new KeyPressEventHandler(txtdos_KeyPress);

txtdoc.KeyPress += new KeyPressEventHandler(txtdoc_KeyPress);

void txtdoc_KeyPress(object sender, KeyPressEventArgs e)

if (!(char.IsDigit(e.KeyChar)) && (e.KeyChar != '\b') && (e.KeyChar != 32) &&


(e.KeyChar != 45) && (e.KeyChar != 47))

e.Handled = true;

MessageBox.Show("Enter Only Dates", "CMS");

txtdoc.Clear();

5
C# WINS APPLICATION SAMPLES
txtdoc.Focus();

void txtdos_KeyPress(object sender, KeyPressEventArgs e)

if (!(char.IsDigit(e.KeyChar)) && (e.KeyChar != '\b') && (e.KeyChar != 32) &&


(e.KeyChar != 45) && (e.KeyChar != 47))

e.Handled = true;

MessageBox.Show("Enter Only Dates", "CMS");

txtdos.Clear();

txtdos.Focus();

void cmbstatus_KeyPress(object sender, KeyPressEventArgs e)

if (cmbstatus.Text.Length > 20)

e.Handled = true;

MessageBox.Show("Entered Text Too Long" + "Only 20 Characters Allowed",


"CMS",

MessageBoxButtons.OK, MessageBoxIcon.Error);

e.Handled = true;

cmbstatus.Clear();

cmbstatus.Focus();

6
C# WINS APPLICATION SAMPLES
void txtaddress_KeyPress(object sender, KeyPressEventArgs e)

if (txtaddress.Text.Length > 49)

e.Handled = true;

MessageBox.Show("Entered Text Too Long" + "Only 50 Characters Allowed",


"CMS",

MessageBoxButtons.OK, MessageBoxIcon.Error);

txtaddress.Clear();

txtaddress.Focus();

void txtname_KeyPress(object sender, KeyPressEventArgs e)

if (!(char.IsLetter(e.KeyChar)) && (e.KeyChar != '\b') && (e.KeyChar !=32) &&


(e.KeyChar !=46))

e.Handled = true;

MessageBox.Show("Enter Only Alphabets","CMS");

txtname.Clear();

txtname.Focus();

if (txtname.Text.Length > 25)

e.Handled = true;

MessageBox.Show("Entered Text Too Long"+"Only 25 Characters


Allowed","CMS",

MessageBoxButtons.OK ,MessageBoxIcon.Error);

txtname.Clear();

txtname.Focus();

7
C# WINS APPLICATION SAMPLES
}

private void btnaddnew_Click(object sender, EventArgs e)

try

string ide = null;

con.Open();

string id = "select BuildingID from Building";

SqlCommand cm = new SqlCommand(id, con);

SqlDataReader dtr = null;

DataSet dst = new DataSet(id);

dtr = cm.ExecuteReader();

while (dtr.Read())

ide = dtr[0].ToString();

string num = ide.Substring(4);

int i = int.Parse(num) + 1;

txtbldid.Text = "BLD-" + i;

txtname.Enabled = true;

txtaddress.Enabled = true;

txtdim.Enabled = true;

dateTimePicker1.Enabled = true;

dateTimePicker2.Enabled = true;

dateTimePicker1.Visible = true;

dateTimePicker2.Visible = true;

radioButton1.Enabled = true;

8
C# WINS APPLICATION SAMPLES
radioButton2.Enabled = true;

cmbstatus.Enabled = true;

cmbengineer.Enabled=true;

btnadd.Enabled = true;

btndel.Enabled = false;

btnupdt.Enabled = false;

con.Close();

catch (Exception ex)

MessageBox.Show(ex.Message, "CMS");

con.Close();

txtname.Clear();

txtaddress.Clear();

txtdim.Clear();

txtdos.Clear();

txtdoc.Clear();

cmbstatus.Text = "";

dataGridView1.Visible = false;

cmbview.Text = "";

cmbengineer.Text = "";

private void btntotview_Click_1(object sender, EventArgs e)

try

con.Open();

9
C# WINS APPLICATION SAMPLES

string tot = "select * from Building";

SqlCommand toc = new SqlCommand(tot, con);

SqlDataAdapter tda = new SqlDataAdapter(toc);

DataSet tds = new DataSet(tot);

tda.Fill(tds, "Building");

dataGridView1.DataSource = tds.Tables["Building"];

dataGridView1.Columns[0].HeaderText = "ID";

con.Close();

groupBox2.Visible = true;

dataGridView1.Show();

catch (Exception ex)

MessageBox.Show(ex.Message, "CMS");

con.Close();

private void btnclear_Click(object sender, EventArgs e)

txtbldid.Clear();

txtname.Clear();

txtaddress.Clear();

txtdim.Clear();

txtdos.Clear();

txtdoc.Clear();

cmbstatus.Text = "";

dateTimePicker1.Visible = false;

dateTimePicker2.Visible = false;

dataGridView1.Visible = false;

10
C# WINS APPLICATION SAMPLES
groupBox2.Visible = false;

this.buildingTableAdapter1.Fill(this.cmsDataSet.Building);

cmbview.Text = "";

cmbengineer.Text = "";

btndel.Enabled = false;

btnupdt.Enabled = false;

private void dateTimePicker2_ValueChanged(object sender, EventArgs e)

txtdoc.Text = dateTimePicker2.Text.ToString();

private void cmbview_SelectedIndexChanged(object sender, EventArgs e)

try

con.Open();

string view = "select * from Building where buildingID='" + cmbview.Text + "'";

SqlCommand comm = new SqlCommand(view, con);

SqlDataReader dtr = null;

DataSet dst = new DataSet(view);

dtr = comm.ExecuteReader();

while (dtr.Read())

txtbldid.Text = dtr["buildingID"].ToString();

txtname.Text = dtr["Name"].ToString();

txtaddress.Text = dtr["Address"].ToString();

txtdim.Text = dtr["Sitedimension"].ToString();

11
C# WINS APPLICATION SAMPLES
txtdos.Text = dtr["workstd"].ToString();

txtdoc.Text = dtr["Dateofcomp"].ToString();

cmbstatus.Text = dtr["status"].ToString();

cmbengineer.Text = dtr["engineer"].ToString();

if (dtr["Type"].ToString() == "MaterialWork ")

radioButton1.Checked = true;

if (dtr["Type"].ToString() == "PieceWork ")

radioButton2.Checked = true;

con.Close();

dateTimePicker2.Visible = true;

dateTimePicker1.Visible = true;

catch (Exception ex)

MessageBox.Show(ex.Message, "CMS");

con.Close();

txtname.Enabled = true;

txtaddress.Enabled = true;

txtdim.Enabled = true;

//txtdos.Enabled = true;

//txtdoc.Enabled = true;

cmbstatus.Enabled = true;

radioButton1.Enabled = true;

radioButton2.Enabled = true;

12
C# WINS APPLICATION SAMPLES
btndel.Enabled = true;

btnupdt.Enabled = true;

dateTimePicker1.Enabled = true;

dateTimePicker2.Enabled = true;

cmbengineer.Enabled=true;

dataGridView1.Visible = false;

cmbview.Text = "";

btnadd.Enabled = false;

private void frmbuild_Load(object sender, EventArgs e)

// TODO: This line of code loads data into the 'cmsDataSet3.Engineer' table.
You can move, or remove it, as needed.

this.engineerTableAdapter.Fill(this.cmsDataSet3.Engineer);

this.buildingTableAdapter1.Fill(this.cmsDataSet.Building);

groupBox2.Visible = false;

dataGridView1.Visible = false;

cmbview.Text = "";

cmbengineer.Text = "";

private void btndel_Click(object sender, EventArgs e)

try

con.Open();

string del = "delete from Building where Buildingid='" + txtbldid.Text + "'";

SqlCommand dell = new SqlCommand(del, con);

SqlDataAdapter dda = new SqlDataAdapter(dell);

13
C# WINS APPLICATION SAMPLES
dell.ExecuteNonQuery();

MessageBox.Show("BUILDING RECORD DELETED", "CMS");

con.Close();

catch (Exception ex)

MessageBox.Show(ex.Message, "CMS");

con.Close();

txtbldid.Clear();

txtname.Clear();

txtaddress.Clear();

txtdim.Clear();

txtdos.Clear();

txtdoc.Clear();

cmbstatus.Text = "";

cmbview.Text = "";

dateTimePicker1.Visible = false;

dateTimePicker2.Visible = false;

btndel.Enabled = false;

btnupdt.Enabled = false;

btnadd.Enabled = false;

this.buildingTableAdapter1.Fill(this.cmsDataSet.Building);

cmbengineer.Text = "";

btndel.Enabled = false;

btnupdt.Enabled = false;

private void btnadd_Click_1(object sender, EventArgs e)

14
C# WINS APPLICATION SAMPLES
string sflag = "0";

if (txtaddress.Text == "" || txtdim.Text == "" || txtdos.Text == "" || txtname.Text


== "")

MessageBox.Show("Data Cannot Be Added As Some Required Field


Data Is Left Blank" +

" Please Fill All The Data which are marked in red",

"CMS", MessageBoxButtons.OK, MessageBoxIcon.Warning);

else

try

con.Open();

string work = null;

if (radioButton1.Checked == true)

work = "MaterialWork";

else if (radioButton2.Checked == true)

work = "PieceWork";

string ins = "insert into building values ('" + txtbldid.Text + "','" +


txtname.Text + "','" + txtaddress.Text + "','" + txtdim.Text + "','" + txtdos.Text + "','" +
txtdoc.Text + "','" + work + "','" + cmbstatus.Text + "','" + cmbengineer.Text + "')";

SqlCommand cmd = new SqlCommand(ins, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

cmd.ExecuteNonQuery();

MessageBox.Show(" BUILDING DATA ADDED SUCCESSFULLY ");

sflag="1";

con.Close();

15
C# WINS APPLICATION SAMPLES
}

catch (Exception ex)

MessageBox.Show(ex.Message, "CMS");

con.Close();

txtbldid.Clear();

txtname.Clear();

txtaddress.Clear();

txtdim.Clear();

txtdos.Clear();

txtdoc.Clear();

cmbstatus.Text = "";

cmbview.Text = "";

cmbengineer.Text = "";

btndel.Enabled = false;

btnupdt.Enabled = false;

if(sflag=="1")

btnadd.Enabled = false;

this.buildingTableAdapter1.Fill(this.cmsDataSet.Building);

private void txtdim_MaskInputRejected(object sender,


MaskInputRejectedEventArgs e)

MessageBox.Show("Wrong Input Type");

private void cmbstatus_SelectedIndexChanged(object sender, EventArgs e)

16
C# WINS APPLICATION SAMPLES
{

private void btnupdt_Click(object sender, EventArgs e)

string sflag = "0";

if (txtaddress.Text == "" || txtdim.Text == "" || txtdos.Text == "" || txtname.Text


== "")

MessageBox.Show("Data Cannot Be Updated As Some Required Field


Data Is Left Blank" +

" Please Fill All The Data which are marked in red",

"CMS", MessageBoxButtons.OK, MessageBoxIcon.Warning);

else

try

con.Open();

string work = null;

if (radioButton1.Checked == true)

work = "MaterialWork";

else

if (radioButton2.Checked == true)

work = "PieceWork";

17
C# WINS APPLICATION SAMPLES
string ins = "UPDATE building set buildingid='" + txtbldid.Text + "',name='" +
txtname.Text + "',address='" + txtaddress.Text + "',sitedimension='" + txtdim.Text +
"',workstd='" + txtdos.Text + "',dateofcomp='" + txtdoc.Text + "',type='" + work +
"',status='" + cmbstatus.Text + "',engineer='" + cmbengineer.Text + "' where buildingid
= '" + txtbldid.Text + "' ";

SqlCommand cmd = new SqlCommand(ins, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

cmd.ExecuteNonQuery();

MessageBox.Show(" BUILDING DATA UPDATED SUCCESSFULLY ");

sflag = "1";

con.Close();

catch (Exception Ex)

MessageBox.Show(Ex.Message, "CMS");

con.Close();

txtbldid.Clear();

txtname.Clear();

txtaddress.Clear();

txtdim.Clear();

txtdos.Clear();

txtdoc.Clear();

cmbstatus.Text = "";

cmbview.Text = "";

cmbengineer.Text = "";

dateTimePicker1.Visible = false;

dateTimePicker2.Visible = false;

btndel.Enabled = false;

if( sflag=="1")

btnupdt.Enabled = false;

btnadd.Enabled = false;

18
C# WINS APPLICATION SAMPLES
this.buildingTableAdapter1.Fill(this.cmsDataSet.Building);

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

txtdos.Text = dateTimePicker1.Text.ToString();

private void dateTimePicker2_ValueChanged_1(object sender, EventArgs e)

txtdoc.Text = dateTimePicker2.Text.ToString();

19

You might also like