-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupplierDetails.cs
219 lines (170 loc) · 6.87 KB
/
SupplierDetails.cs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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 Science
{
public partial class SupplierDetails : Form
{
string suppID;
int intsuppid;
int stockitemid;
int lastRecord;
public SupplierDetails()
{
InitializeComponent();
}
public SupplierDetails(string sID, int equipId)
{
InitializeComponent();
suppID = sID;
intsuppid = Convert.ToInt32(suppID, 10);
stockitemid = equipId;
}
private void supplierTableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.supplierTableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.sciencedbDataSet);
}
private void supplierTableBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.supplierTableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.sciencedbDataSet);
}
private void SupplierDetails_Load(object sender, EventArgs e)
{
//We don't need a time for the last purchase value.
lastPurchaseDateTimePicker.Format = DateTimePickerFormat.Custom;
lastPurchaseDateTimePicker.CustomFormat = "dd 'of' MMMM yyyy";
string filterExp = "ID = '" + suppID + "'";
supplierTableBindingSource.Filter = filterExp;
sciencedbDataSet.SupplierTableDataTable set = this.sciencedbDataSet.SupplierTable;
this.supplierTableTableAdapter.Fill(set);
//Do the combo dance
sciencedbDataSet.SupplierTableDataTable table = supplierTableTableAdapter.GetData();
if (intsuppid > 1)
{
suppcombo.DataSource = table;
suppcombo.DisplayMember = "SupplierName";
suppcombo.ValueMember = "ID";
}
}
public void updatecombobox()
{
object selectedItem = suppcombo.SelectedItem;
DataRowView rowView = (DataRowView)selectedItem;
DataRow row = rowView.Row;
suppID = row.ItemArray[0].ToString();
string filterExp = "ID = '" + suppID + "'";
supplierTableBindingSource.Filter = filterExp;
this.Update();
}
public void saveValues_Click(object sender, EventArgs e)
{
if (intsuppid > 0)
{
string updatequery = "Update SupplierTable SET Cost = {0},Quantity = {1}, SupplierName = '{2}', LastPurchase='{3}' WHERE ID = {4}";
updatequery = string.Format(updatequery, this.costTextBox.Text, this.quantityTextBox.Text, this.supplierNameTextBox.Text, this.lastPurchaseDateTimePicker.Value, intsuppid);
runCommand(updatequery);
}
else
{
string insertquery = "Insert INTO supplierTable(Cost,Quantity,LastPurchase,SupplierName) Values({0},{1},'{2}','{3}')";
insertquery = string.Format(insertquery, costTextBox.Text,quantityTextBox.Text,lastPurchaseDateTimePicker.Value,supplierNameTextBox.Text);
runCommand(insertquery);
//Get the next stockid from suppliertable
lastRecord = 0;
getlastitem();
string updatesuppid = "Update EquipmentTable Set SupplierID = {0} WHERE id = {1}";
updatesuppid = string.Format(updatesuppid, lastRecord, stockitemid);
runCommand(updatesuppid);
}
this.Close();
}
private void cancelbutton_Click(object sender, EventArgs e)
{
this.Close();
}
private void getlastitem()
{
//Get the next stockid from suppliertable
//get last record so we know what the new item will be.
OleDbConnection mycon2 = new OleDbConnection();
mycon2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\sciencedb.accdb";
lastRecord = 0;
OleDbCommand cmdUpdate = new OleDbCommand();
string sGetLastItem = "select TOP 1 * from SupplierTable order by id desc";
OleDbCommand getToprecord = new OleDbCommand();
getToprecord.CommandText = sGetLastItem;
OleDbDataReader reader = null;
try
{
getToprecord.Connection = mycon2;
mycon2.Open();
reader = getToprecord.ExecuteReader();
while (reader.Read())
{
lastRecord = (int)reader[0];
}
}
finally
{
mycon2.Close();
}
}
private void runCommand(string addCommand)
{
int iSqlStatus;
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\sciencedb.accdb";
OleDbCommand oledbRunCommand = new OleDbCommand();
oledbRunCommand.CommandText = addCommand;
oledbRunCommand.CommandType = CommandType.Text;
try
{
oledbRunCommand.Connection = connection;
connection.Open();
iSqlStatus = oledbRunCommand.ExecuteNonQuery();
if (iSqlStatus != 0)
{
MessageBox.Show("Record Updated");
//return false;
}
else
{
MessageBox.Show("Failed to Update record. Please check the details entered.");
//return true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
connection.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
;
//suppcombo.SelectedItem;
DataRowView row = (DataRowView)suppcombo.SelectedItem;
object name = row[1];
object myobject = row[1];
// suppcombo.SelectedValue[0] Cannot apply indexing with [] to an expression of type 'object'
}
private void suppcombo_SelectedIndexChanged(object sender, EventArgs e)
{
if (suppcombo.SelectedIndex != -1)
updatecombobox();
}
}
}