-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm16.cs
More file actions
178 lines (152 loc) · 7.05 KB
/
Form16.cs
File metadata and controls
178 lines (152 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DatabaseProject
{
public partial class Form16 : Form
{
//const string constr = @"Data Source=DESKTOP-SIPFLUH\DBSML3;Initial Catalog=dbproj;Integrated Security=SSPI";
const string constr = @"Data Source = ROHAN-PC\SPARTA; Initial Catalog = dbproj ;Integrated Security = SSPI";
SqlConnection con = new SqlConnection(constr);
SqlCommand cm = new SqlCommand();
public Form16()
{
InitializeComponent();
}
private void LoadCustomers()
{
// TODO: Complete the function LoadOrders
// SQL Query to Select all records from orders table in the descending order of order date
con.Open();
string sql = "select * from [customerdetails]";
cm = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cm);
DataTable d = new DataTable();
da.Fill(d);
cm.Dispose();
con.Close();
dataGridView1.DataSource = d;
dataGridView1.BackgroundColor = Color.White;
dataGridView1.RowHeadersVisible = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
/* private void DeleteCustomer(string customer_id)
{
con.Open();
// SQL query to Delete the from the order and order details table
string sql = "Delete [BookingTranscaction] where @booking_id = select booking_id from BookingDetails where @customer_id = customer_id; Delete [BookingDetails] where @customer_id = customer_id; Delete [CustomerDetails] where @customer_id = customer_id;";
cm = new SqlCommand(sql, con);
// Specify the value of the parameters
cm.Parameters.AddWithValue("@customer_id", customer_id);
cm.Parameters.AddWithValue("@booking_id", select booking_id from BookingDetails where @customer_id = customer_id);
cm.ExecuteNonQuery();
cm.Dispose();
con.Close();
} */
private void Form16_Load(object sender, EventArgs e)
{
dataGridView1.BackgroundColor = Color.White;
dataGridView1.RowHeadersVisible = false;
/* var deleteButton = new DataGridViewButtonColumn();
deleteButton.Name = "dataGridViewDeleteButton";
deleteButton.HeaderText = "Delete";
deleteButton.Text = "Delete";
deleteButton.UseColumnTextForButtonValue = true; */
LoadCustomers();
//dataGridView1.Columns.Add(deleteButton);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//tbcustomer_id.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
tbfirst_name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
tblast_name.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
tbphone_number.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
tbaddress.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
//if click is on new row or header row
/* if (e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
return;
//Check if click is on specific column
if (e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
{
var data = dataGridView1.Rows[e.RowIndex];
MessageBox.Show("Deleting");
DeleteCustomer(data.Cells[0].Value.ToString());
dataGridView1.Columns.Remove("dataGridViewDeleteButton");
dataGridView1.DataSource = null;
LoadCustomers();
var deleteButton = new DataGridViewButtonColumn();
deleteButton.Name = "dataGridViewDeleteButton";
deleteButton.HeaderText = "Delete";
deleteButton.Text = "Delete";
deleteButton.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(deleteButton);
} */
}
private void InsertCustomer()
{
// Sql query to insert a new supplier.
string sql = "Insert Into [CustomerDetails] ([customer_id],[first_name],[last_name], [phone_number], [address]) values ((select max(customer_id) from CustomerDetails) + 1, @first_name, @last_name, @phone_number, @address)";
cm = new SqlCommand(sql, con);
// Specify the value of the parameters
con.Open();
cm = new SqlCommand(sql, con);
// Specify the value for the parameters.
if (tbfirst_name.TextLength > 0 && tblast_name.TextLength > 0 && tbphone_number.TextLength > 0 && tbaddress.TextLength > 0)
{
//cm.Parameters.AddWithValue("@customer_id", tbcustomer_id.Text);
cm.Parameters.AddWithValue("@first_name", tbfirst_name.Text);
cm.Parameters.AddWithValue("@last_name", tblast_name.Text);
cm.Parameters.AddWithValue("@phone_number", tbphone_number.Text);
cm.Parameters.AddWithValue("@address", tbaddress.Text);
//dataGridView1.Rows.Add(tbSupplierID.Text.ToString(), tbSupplierName.Text.ToString());
}
else
{
MessageBox.Show("Please provide valid information!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//tbcustomer_id.Text = "";
tbfirst_name.Text = "";
tblast_name.Text = "";
tbphone_number.Text = "";
tbaddress.Text = "";
cm.ExecuteNonQuery();
con.Close();
}
private void btnInsert_Click(object sender, EventArgs e)
{
InsertCustomer();
MessageBox.Show("Customer has been successfully inserted.");
//tbcustomer_id.Text = "";
tbfirst_name.Text = "";
tblast_name.Text = "";
tbphone_number.Text = "";
tbaddress.Text = "";
this.Close();
Form16 f = new Form16();
f.Show();
}
private void btnClear_Click(object sender, EventArgs e)
{
//tbcustomer_id.Text = "";
tbfirst_name.Text = "";
tblast_name.Text = "";
tbphone_number.Text = "";
tbaddress.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form3 f = new Form3(f1);
f.Show();
this.Close();
}
}
}