to develop simple window application with database "customer.mdb" and "cust" table with customer name,address,town,country,postal code.
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.OleDb;
namespace _2013
{
public partial class Form1 : Form
{
OleDbCommand cmd;
OleDbConnection con;
string query = "";
int f = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\anuj vb 10\2013\Customer.mdb");
label1.Text = "Customer name";
label2.Text = "address";
label3.Text = "town";
label4.Text = "country";
label5.Text = "postal code";
button1.Text = "save";
button2.Text = "update";
button3.Text = "delete";
}
private void button1_Click(object sender, EventArgs e)
{
check();
if (f == 0)
{
MessageBox.Show("insert data in all fields");
return;
}
query="insert into cust values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"')";
con.Open();
cmd = new OleDbCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("data entered");
con.Close();
}
public void check()
{
f = 1;
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
if (((TextBox)c).Text.Trim().Length <= 0)
f = 0;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
check();
if (f == 0)
{
MessageBox.Show("insert data in all fields");
return;
}
query = "update cust set address ='" + textBox2.Text + "', town ='" + textBox3.Text + "', country ='" + textBox4.Text + "', postal_code ='" + textBox5.Text + "' where Customer_name ='"+textBox1.Text+"'";
con.Open();
cmd = new OleDbCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("data updated");
else
MessageBox.Show("record not found");
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length < 0)
{
MessageBox.Show("insert data in name field");
return;
}
query = "delete from cust where Customer_name ='" + textBox1.Text + "'";
con.Open();
cmd = new OleDbCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("data deleted");
else
MessageBox.Show("record not found");
con.Close();
}
}
}
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.OleDb;
namespace _2013
{
public partial class Form1 : Form
{
OleDbCommand cmd;
OleDbConnection con;
string query = "";
int f = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\anuj vb 10\2013\Customer.mdb");
label1.Text = "Customer name";
label2.Text = "address";
label3.Text = "town";
label4.Text = "country";
label5.Text = "postal code";
button1.Text = "save";
button2.Text = "update";
button3.Text = "delete";
}
private void button1_Click(object sender, EventArgs e)
{
check();
if (f == 0)
{
MessageBox.Show("insert data in all fields");
return;
}
query="insert into cust values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"')";
con.Open();
cmd = new OleDbCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("data entered");
con.Close();
}
public void check()
{
f = 1;
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
if (((TextBox)c).Text.Trim().Length <= 0)
f = 0;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
check();
if (f == 0)
{
MessageBox.Show("insert data in all fields");
return;
}
query = "update cust set address ='" + textBox2.Text + "', town ='" + textBox3.Text + "', country ='" + textBox4.Text + "', postal_code ='" + textBox5.Text + "' where Customer_name ='"+textBox1.Text+"'";
con.Open();
cmd = new OleDbCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("data updated");
else
MessageBox.Show("record not found");
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length < 0)
{
MessageBox.Show("insert data in name field");
return;
}
query = "delete from cust where Customer_name ='" + textBox1.Text + "'";
con.Open();
cmd = new OleDbCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
MessageBox.Show("data deleted");
else
MessageBox.Show("record not found");
con.Close();
}
}
}
Comments