-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmExpenseRecord.cs
383 lines (294 loc) · 12.2 KB
/
frmExpenseRecord.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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;
using Excel = Microsoft.Office.Interop.Excel;
namespace Sales_and_Inventory_System__Gadgets_Shop_
{
public partial class frmExpenseRecord : Form
{
SqlDataReader rdr;
DataTable dTable;
SqlConnection con = null;
SqlDataAdapter adp;
DataSet ds;
SqlCommand cmd = null;
DataTable dt= new DataTable();
ConnectionString cs = new ConnectionString();
public frmExpenseRecord()
{
InitializeComponent();
}
public void FillCombo()
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
adp = new SqlDataAdapter();
adp.SelectCommand = new SqlCommand("SELECT distinct SupplierName FROM Supplier ", con);
ds = new DataSet("ds");
adp.Fill(ds);
dTable = ds.Tables[0];
cmbCustomerName.Items.Clear();
foreach (DataRow drow in dTable.Rows)
{
cmbCustomerName.Items.Add(drow[0].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void frmExpenseRecord_Load(object sender, EventArgs e)
{
FillCombo();
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
GroupBox3.Visible = true;
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ExpenseNo) as [Expense ID],RTRIM(ExpenseDate) as [Expense Date],RTRIM(ExpencseDetail) as [Expense Details],RTRIM(Amount) as [Expense Amount],RTRIM(ReturnAmount) as [Return Amount],RTRIM(RADetails) as [Retuen Amount Details] from Expense where ExpenseDate between @d1 and @d2 order by ExpenseDate desc", con);
cmd.Parameters.Add("@d1", SqlDbType.DateTime, 30, "ExpenseDate").Value = dtpInvoiceDateFrom.Value.Date;
cmd.Parameters.Add("@d2", SqlDbType.DateTime, 30, "ExpenseDate").Value = dtpInvoiceDateTo.Value.Date;
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "Expense");
DataGridView1.DataSource = myDataSet.Tables["Expense"].DefaultView;
Decimal sum = 0;
Decimal sum1 = 0;
foreach (DataGridViewRow r in this.DataGridView1.Rows)
{
Decimal i = Convert.ToDecimal(r.Cells[3].Value);
Decimal j = Convert.ToDecimal(r.Cells[4].Value);
sum = sum + i;
sum1 = sum1 + j;
}
textBox3.Text = sum.ToString();
textBox4.Text = sum1.ToString();
txtCustomerID.Text = Convert.ToDecimal((Convert.ToDecimal(textBox3.Text) - Convert.ToDecimal(textBox4.Text))).ToString();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
}
private void GroupBox3_Enter(object sender, EventArgs e)
{
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
private void txtCustomerID_TextChanged(object sender, EventArgs e)
{
}
private void TabPage1_Click(object sender, EventArgs e)
{
}
private void Button3_Click(object sender, EventArgs e)
{
if (DataGridView1.DataSource == null)
{
MessageBox.Show("Sorry nothing to export into excel sheet..", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rowsTotal = 0;
int colsTotal = 0;
int I = 0;
int j = 0;
int iC = 0;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
Excel.Application xlApp = new Excel.Application();
try
{
Excel.Workbook excelBook = xlApp.Workbooks.Add();
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[1];
xlApp.Visible = true;
rowsTotal = DataGridView1.RowCount - 1;
colsTotal = DataGridView1.Columns.Count - 1;
var _with1 = excelWorksheet;
_with1.Cells.Select();
_with1.Cells.Delete();
for (iC = 0; iC <= colsTotal; iC++)
{
_with1.Cells[1, iC + 1].Value = DataGridView1.Columns[iC].HeaderText;
}
for (I = 0; I <= rowsTotal - 1; I++)
{
for (j = 0; j <= colsTotal; j++)
{
_with1.Cells[I + 2, j + 1].value = DataGridView1.Rows[I].Cells[j].Value;
}
}
_with1.Rows["1:1"].Font.FontStyle = "Bold";
_with1.Rows["1:1"].Font.Size = 12;
_with1.Cells.Columns.AutoFit();
_with1.Cells.Select();
_with1.Cells.EntireColumn.AutoFit();
_with1.Cells[1, 1].Select();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
//RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
xlApp = null;
}
}
private void Button7_Click(object sender, EventArgs e)
{
}
private void Button2_Click(object sender, EventArgs e)
{
DataGridView1.DataSource = null;
dtpInvoiceDateFrom.Text = DateTime.Today.ToString();
dtpInvoiceDateTo.Text = DateTime.Today.ToString();
GroupBox3.Visible = false;
DataGridView1.DataSource = null;
// dataGridView5.DataSource = null;
}
private void Button9_Click(object sender, EventArgs e)
{
DataGridView3.DataSource = null;
cmbCustomerName.Text = "";
groupBox4.Visible = false;
// dataGridView6.DataSource = null;
}
private void GroupBox4_Enter(object sender, EventArgs e)
{
}
private void cmbCustomerName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
groupBox4.Visible = true;
cmbCustomerName.Text = cmbCustomerName.Text.Trim();
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ExpenseNo) as [Bill ID],RTRIM(SuplierName) as [Name],RTRIM(ExpenseDate) as [Expense Date],RTRIM(ExpencseDetail) as [Expense Details],RTRIM(Total) as [Total Amount],RTRIM(Amount) as [Amount Received],RTRIM(Remaining) as [Remaining Amount] from personalExpense where ExpenseDate between @d1 and @d2 and SuplierName='" + cmbCustomerName.Text.Trim() + "' order by ExpenseDate", con);
cmd.Parameters.Add("@d1", SqlDbType.DateTime, 30, "ExpenseDate").Value = dateTimePicker2.Value.Date;
cmd.Parameters.Add("@d2", SqlDbType.DateTime, 30, "ExpenseDate").Value = dateTimePicker1.Value.Date;
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "personalExpense");
DataGridView3.DataSource = myDataSet.Tables["personalExpense"].DefaultView;
Int64 sum = 0;
Int64 sum1 = 0;
foreach (DataGridViewRow r in this.DataGridView3.Rows)
{
Int64 i = Convert.ToInt64(r.Cells[4].Value);
Int64 j = Convert.ToInt64(r.Cells[5].Value);
sum = sum + i;
sum1 = sum1 + j;
}
textBox6.Text = sum.ToString();
textBox8.Text = sum1.ToString();
textBox9.Text = Convert.ToDecimal((Convert.ToDecimal(textBox6.Text) - Convert.ToDecimal(textBox8.Text))).ToString();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void DataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void TabPage3_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
}
private void textBox7_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click_1(object sender, EventArgs e)
{
if (DataGridView3.DataSource == null)
{
MessageBox.Show("Sorry nothing to export into excel sheet..", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rowsTotal = 0;
int colsTotal = 0;
int I = 0;
int j = 0;
int iC = 0;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
Excel.Application xlApp = new Excel.Application();
try
{
Excel.Workbook excelBook = xlApp.Workbooks.Add();
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[1];
xlApp.Visible = true;
rowsTotal = DataGridView3.RowCount - 1;
colsTotal = DataGridView3.Columns.Count - 1;
var _with1 = excelWorksheet;
_with1.Cells.Select();
_with1.Cells.Delete();
for (iC = 0; iC <= colsTotal; iC++)
{
_with1.Cells[1, iC + 1].Value = DataGridView3.Columns[iC].HeaderText;
}
for (I = 0; I <= rowsTotal - 1; I++)
{
for (j = 0; j <= colsTotal; j++)
{
_with1.Cells[I + 2, j + 1].value = DataGridView3.Rows[I].Cells[j].Value;
}
}
_with1.Rows["1:1"].Font.FontStyle = "Bold";
_with1.Rows["1:1"].Font.Size = 12;
_with1.Cells.Columns.AutoFit();
_with1.Cells.Select();
_with1.Cells.EntireColumn.AutoFit();
_with1.Cells[1, 1].Select();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
//RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
xlApp = null;
}
}
}
}