Sei sulla pagina 1di 5

using System;

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

namespace lab1_sql
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

}
//New Line
public void refresh(){
SqlDataAdapter da = new SqlDataAdapter();
string str = "Data Source=DESKTOP-U0CH9PL\\MSSQLSERVER1;Initial
Catalog=customer_maintenance;Integrated Security=True";

SqlConnection con = new SqlConnection(str);


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Tbl_Student";
//cmd.Parameters.AddWithValue("@param9", searchtextBox.Text);
con.Open();
cmd.ExecuteNonQuery();
da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.SelectCommand = cmd;
da.Fill(table);
dataGridView1.DataSource = table;
dataGridView1.Refresh();
con.Close();

}
private void button1_Click(object sender, EventArgs e)
{
if (txtid.Text == string.Empty)
{
MessageBox.Show("please enter customer id");
}
SqlConnection con;
SqlCommand cmd;
string sql = null;

string str = "Data Source=DESKTOP-U0CH9PL\\MSSQLSERVER1;Initial


Catalog=customer_maintenance;Integrated Security=True";
sql = "SELECT * FROM tbl_customer where customer_id=@val";
con = new SqlConnection(str);
try
{
con.Open();
cmd = new SqlCommand(sql, con);
//These are params that you give when searching for a data or
taking id from user on basis of
// which you will get record
cmd.Parameters.AddWithValue("@val", Convert.ToInt32(txtid.Text));
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//dr.GetValue(0) means get 0th cell value in textbox1
txtname.Text = dr.GetValue(1).ToString();
txtaddress.Text = dr.GetValue(2).ToString();
txtcity.Text = dr.GetValue(3).ToString();
txtstate.Text = dr.GetValue(4).ToString();
txtzipcode.Text = dr.GetValue(5).ToString();
}
dr.Close();
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}

private void Form1_Load(object sender, EventArgs e)


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

this.tbl_customerTableAdapter.Fill(this.customer_maintenanceDataSet.tbl_customer);

private void btnadd_Click(object sender, EventArgs e)


{
if (txtid.Text == string.Empty)
{
MessageBox.Show("please enter customer id");
}
else if (txtname.Text == string.Empty)
{
MessageBox.Show("please enter customer name");
}
else if (txtaddress.Text == string.Empty)
{
MessageBox.Show("please enter customer address");
}
else if (txtcity.Text == string.Empty)
{
MessageBox.Show("please enter customer city");
}
else if (txtstate.Text == string.Empty)
{
MessageBox.Show("please enter customer state");
}
else if (txtzipcode.Text == string.Empty)
{
MessageBox.Show("please enter customer zipcode");
}
else
{

SqlDataAdapter da = new SqlDataAdapter();


string str = "Data Source=DESKTOP-U0CH9PL\\MSSQLSERVER1;Initial
Catalog=customer_maintenance;Integrated Security=True";

SqlConnection con = new SqlConnection(str);


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO tbl_customer(customer_id,name,
address,city,state,zipcode) VALUES
(@param1,@param2,@param3,@param4,@param5,@param6)";
cmd.Parameters.AddWithValue("@param1", txtid.Text);
cmd.Parameters.AddWithValue("@param2", txtname.Text);
cmd.Parameters.AddWithValue("@param3", txtaddress.Text);
cmd.Parameters.AddWithValue("@param4", txtcity.Text);
cmd.Parameters.AddWithValue("@param5", txtstate.Text);
cmd.Parameters.AddWithValue("@param6", txtzipcode.Text);

con.Open();

cmd.ExecuteNonQuery();
//New Line
refresh();

private void btnmodify_Click(object sender, EventArgs e)


{
if (txtid.Text == string.Empty)
{
MessageBox.Show("please enter customer id");
}
else if (txtname.Text == string.Empty)
{
MessageBox.Show("please enter customer name");
}
else if (txtaddress.Text == string.Empty)
{
MessageBox.Show("please enter customer address");
}
else if (txtcity.Text == string.Empty)
{
MessageBox.Show("please enter customer city");
}
else if (txtstate.Text == string.Empty)
{
MessageBox.Show("please enter customer state");
}
else if (txtzipcode.Text == string.Empty)
{
MessageBox.Show("please enter customer zipcode");
}
else
{

string str = "Data Source=DESKTOP-U0CH9PL\\MSSQLSERVER1;Initial


Catalog=customer_maintenance;Integrated Security=True";

SqlConnection con = new SqlConnection(str);


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE tbl_customer SET customer_id =
@param1,name =@param2, address =@param3,city= @param4,state=@param5,zipcode=
@param6 WHERE customer_id = @param1";
cmd.Parameters.AddWithValue("@param1", txtid.Text);
cmd.Parameters.AddWithValue("@param2", txtname.Text);
cmd.Parameters.AddWithValue("@param3", txtaddress.Text);
cmd.Parameters.AddWithValue("@param4", txtcity.Text);
cmd.Parameters.AddWithValue("@param5", txtstate.Text);
cmd.Parameters.AddWithValue("@param6", txtzipcode.Text);

con.Open();
cmd.ExecuteNonQuery();
//New Line
refresh();

}
}

private void btndel_Click(object sender, EventArgs e)


{
DialogResult clr = MessageBox.Show("are you sure you want to delete",
"confirm delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (clr == DialogResult.OK)
{

string str = "Data Source=DESKTOP-U0CH9PL\\MSSQLSERVER1;Initial


Catalog=customer_maintenance;Integrated Security=True";

SqlConnection con = new SqlConnection(str);


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "DELETE FROM tbl_customer WHERE customer_id =
@param1";
cmd.Parameters.AddWithValue("@param1", txtid.Text);

con.Open();
cmd.ExecuteNonQuery();

refresh();

}
else
{
MessageBox.Show("you have clicked cancel button");

private void btnexit_Click(object sender, EventArgs e)


{
this.Close();

private void dataGridView1_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{

� � � �
� � � � � �

� � � �

}
}
}

Potrebbero piacerti anche