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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=ARNAV;Initial Catalog=Mrudu;User ID=sa;Password=manager");
SqlCommand cmd;
SqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into Cust values("+textBox1.Text+",'"+textBox2.Text+"')", con);
int r=cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("Row Inserted!!!");
else
MessageBox.Show("Insert Data");
con.Close();
display();
}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
cmd=new SqlCommand("delete from Cust where Cust_ID="+textBox1.Text);
cmd.Connection = con;
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("Row Deleted!!!");
else
MessageBox.Show("Not Deleted");
con.Close();
display();
}
private void button3_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Employee set Emp_Name='"+textBox2.Text+"' where Emp_ID ="+textBox1.Text);
cmd.Connection = con;
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("Row updated");
else
MessageBox.Show("Not updated");
con.Close();
//display();
}
private void button4_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("select count(Cust_ID) from Cust", con);
int cnt = (int)cmd.ExecuteScalar();
MessageBox.Show("Total No. of customers : "+cnt.ToString());
con.Close();
}
private void button5_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("StoredProcedure1", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
param.ParameterName = "@cno";
param.Value = textBox1.Text;
cmd.Parameters.Add(param);
dr = cmd.ExecuteReader();
while (dr.Read())
{
MessageBox.Show("ID : " + dr[0].ToString());
MessageBox.Show("Name : " + dr[1].ToString());
}
con.Close();
//display();
}
public void display()
{
con.Open();
cmd = new SqlCommand("select * from Cust", con);
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
display();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=ARNAV;Initial Catalog=Mrudu;User ID=sa;Password=manager");
SqlCommand cmd;
SqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into Cust values("+textBox1.Text+",'"+textBox2.Text+"')", con);
int r=cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("Row Inserted!!!");
else
MessageBox.Show("Insert Data");
con.Close();
display();
}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
cmd=new SqlCommand("delete from Cust where Cust_ID="+textBox1.Text);
cmd.Connection = con;
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("Row Deleted!!!");
else
MessageBox.Show("Not Deleted");
con.Close();
display();
}
private void button3_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Employee set Emp_Name='"+textBox2.Text+"' where Emp_ID ="+textBox1.Text);
cmd.Connection = con;
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("Row updated");
else
MessageBox.Show("Not updated");
con.Close();
//display();
}
private void button4_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("select count(Cust_ID) from Cust", con);
int cnt = (int)cmd.ExecuteScalar();
MessageBox.Show("Total No. of customers : "+cnt.ToString());
con.Close();
}
private void button5_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("StoredProcedure1", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
param.ParameterName = "@cno";
param.Value = textBox1.Text;
cmd.Parameters.Add(param);
dr = cmd.ExecuteReader();
while (dr.Read())
{
MessageBox.Show("ID : " + dr[0].ToString());
MessageBox.Show("Name : " + dr[1].ToString());
}
con.Close();
//display();
}
public void display()
{
con.Open();
cmd = new SqlCommand("select * from Cust", con);
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
display();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}