/
Default.aspx.cs
162 lines (129 loc) · 4.71 KB
/
Default.aspx.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
namespace SMSU_PO
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if ((string)Session["StarID"] != null)
{
GridView1.DataSource = involvementSelect();
GridView1.DataBind();
btnDownload.Visible = false;
}
else
{
Response.Redirect("Login.aspx");
}
}
protected DataSet purchase()
{
DataSet ds = new DataSet();
poDll.PurchaseOrder dll = new poDll.PurchaseOrder();
ds = dll.PURCHASE_Select((string)Session["StarID"]);
return ds;
}
protected DataSet involvementSelect()
{
poDll.PurchaseOrder selectInv = new poDll.PurchaseOrder();
DataSet ds = new DataSet();
ds = selectInv.INVOLVEMENT_Select((string)Session["StarID"]);
return ds;
}
protected void btnViewPurchase_Click(object sender, EventArgs e)
{
poDll.PurchaseOrder selectInv = new poDll.PurchaseOrder();
DataSet ds = new DataSet();
int poNum = Int32.Parse(dlPurchases.Text);
ds = selectInv.PURCHASE_Select(poNum);
GridView3.DataSource = ds;
GridView3.DataBind();
btnDownload.Visible = true;
}
protected void btnDownload_Click(object sender, EventArgs e)
{
poDll.PurchaseOrder selectInv = new poDll.PurchaseOrder();
DataSet ds = new DataSet();
int poNum = Int32.Parse(dlPurchases.Text);
ds = selectInv.PURCHASE_Select(poNum);
if (ds.Tables[0].Rows.Count < 1)
{
lblError.Text = "No results. Could not download.";
}
else
{
Download(ds);
}
}
protected bool Download(DataSet ds)
{
//DataRow Row;
//DataColumn Col;
string szFileName;
string dwFileName;
DateTime todaysDte = DateTime.Today;
string dte = todaysDte.ToString("MM-dd-yyyy");
DirectoryInfo oDir = new DirectoryInfo(Server.MapPath("DownloadFiles"));
FileInfo[] oFileArray = oDir.GetFiles();
//FileInfo oFileInfo;
try
{
foreach (FileInfo oFileInfo in oFileArray)
{
if ((oFileInfo.Extension.ToLower() == ".csv"))
{
oFileInfo.Delete();
}
}
}
catch (Exception e)
{
throw e;
}
szFileName = Server.MapPath("DownloadFiles") + "\\Purchase " + dte + ".csv";
dwFileName = "\\Purchase " + dte + ".csv";
if (System.IO.File.Exists(szFileName))
{
System.IO.File.Delete(szFileName);
}
System.IO.StreamWriter sWriter = new System.IO.StreamWriter(szFileName);
string strHeadings = "";
string strData = "";
foreach (DataColumn Col in ds.Tables[0].Columns)
{
strHeadings += Col.ColumnName + ",";
}
//Remove Trailing Comma
strHeadings = strHeadings.Substring(0, (strHeadings.Length - 1));
//Write the headings to the file
sWriter.WriteLine(strHeadings);
foreach (DataRow Row in ds.Tables[0].Rows)
{
foreach (DataColumn Col in Row.Table.Columns)
{
// strData &= Row(Col.ColumnName).ToString & ","
strData = (strData + Row[Col.ColumnName].ToString() + ",");
}
//Remove Trailing Comma
strData = strData.Substring(0, (strData.Length - 1));
//Write Line
sWriter.WriteLine(strData);
strData = "";
}
sWriter.Close();
//Start the download
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + dwFileName);
Response.TransmitFile(Server.MapPath("DownloadFiles" + dwFileName));
Response.End();
return true;
}
}
}