-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVFileUploader.aspx.cs
320 lines (276 loc) · 12.8 KB
/
CSVFileUploader.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
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using DA_SKORKEL;
using System.Text;
using System.Net;
//using Microsoft.VisualBasic.FileIO;
public partial class CSVFileUploader : System.Web.UI.Page
{
//DO_Registrationdetails objRegistration = new DO_Registrationdetails();
//DA_Registrationdetails objRegistrationDB = new DA_Registrationdetails();
DA_Login objLoginDB = new DA_Login();
DO_Login objLogin = new DO_Login();
Random rd = new Random();
DO_Registrationdetails objdoreg = new DO_Registrationdetails();
DA_Registrationdetails objdareg = new DA_Registrationdetails();
DO_Scrl_APILogDetailsTbl objAPILogDO = new DO_Scrl_APILogDetailsTbl();
DA_Scrl_APILogDetailsTbl objAPILogDA = new DA_Scrl_APILogDetailsTbl();
string APIURL = ConfigurationManager.AppSettings["APIURL"];
string ISAPIURLACCESSED = ConfigurationManager.AppSettings["ISAPIURLACCESSED"];
string ISAPIResponse = ConfigurationManager.AppSettings["ISAPIResponse"];
protected void Page_Load(object sender, EventArgs e)
{
lblError.Visible = false;
}
protected void btnDownload_Click(object sender, EventArgs e)
{
string dwnload = "~/Downloads/SqlExport.csv";
string filePath = dwnload;
Response.AddHeader("Content-Disposition", "attachment;filename=\"Dummy.csv\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}
//File Upload Control click event
protected void btnUpload_Click(object sender, EventArgs e)
{
//Creating objects of datatable
DataTable tblcsv = new DataTable();
DataTable dt = new DataTable();
DataTable dtCheck = new DataTable();
DataTable dt3 = new DataTable();
//Creating columns
tblcsv.Columns.Add("FirstName");
tblcsv.Columns.Add("LastName");
tblcsv.Columns.Add("Email");
string root = Server.MapPath("/FilesUploads/");
//string subdir = @"C:\Temp\Mahesh";
// If directory does not exist, create it.
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
//Getting full file path of Uploaded file
string CSVFilePath = null;
CSVFilePath = Server.MapPath("~/FilesUploads/") + Path.GetFileName(fileUpload.PostedFile.FileName);
if (fileUpload.HasFile)
// Call a helper method routine to save the file.
{
fileUpload.SaveAs(CSVFilePath);
string ReadCSV = File.ReadAllText(CSVFilePath);
foreach (string csvRow in ReadCSV.Split('\n'))
{
if (!string.IsNullOrEmpty(csvRow))
{
//Adding each row into datatable
tblcsv.Rows.Add();
int count = 0;
foreach (string FileRec in csvRow.Split(','))
{
tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
count++;
}
}
}
//Getting datatable without header
dt = tblcsv.Clone();
foreach (DataRow drtableOld in tblcsv.AsEnumerable().Skip(1))
{
dt.ImportRow(drtableOld);
}
DataColumn myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.AllowDBNull = false;
myColumn.Caption = "vchrActive";
myColumn.ColumnName = "vchrActive";
myColumn.DefaultValue = "Y";
dt.Columns.Add(myColumn);
DataColumn myColumnpwd = new DataColumn();
myColumnpwd.DataType = System.Type.GetType("System.String");
myColumnpwd.AllowDBNull = false;
myColumnpwd.Caption = "vchrPassword";
myColumnpwd.ColumnName = "vchrPassword";
//myColumnpwd.DefaultValue = pwd;
dt.Columns.Add(myColumnpwd);
dt3 = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
objLogin.Username = Convert.ToString(dt.Rows[i]["Email"]).Replace("\r", null);
dtCheck.Clear();
dtCheck = objLoginDB.GetDataSet(objLogin, DA_SKORKEL.DA_Login.Login_1.GetUserMailCheck);
if (dtCheck.Rows.Count > 0)
{
//lblMsgs.Text = "Email id already exist.";
//txtUname.Text = "";
//return;
}
else
{
string pwd = null;
pwd = CreateRandomPassword(10);
dt.Rows[i]["vchrPassword"] = pwd;
dt3.ImportRow(dt.Rows[i]);
}
}
//Calling insert Functions
InsertCSVRecords(dt3);
}
else
// Notify the user that a file was not uploaded.
{
lblError.Visible = true;
lblError.Text = "You did not specify a file to upload.";
}
}
//Getting random password
private string CreateRandomPassword(int passwordLength)
{
string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
string numbers = "0123456789";
char[] chars = new char[passwordLength];
//Random rd = new Random();
for (int i = 0; i < passwordLength-2; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
for (int i = 0; i < 2; i++)
{
chars[passwordLength-2+i] = numbers[rd.Next(0, numbers.Length)];
}
return new string(chars);
}
//Function to Insert Records
private void InsertCSVRecords(DataTable csvdt)
{
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
//bool IsSuccessSave = false;
//SqlTransaction transaction = null;
DataTable dtInsrt = new DataTable();
dtInsrt = csvdt.Clone();
DataColumn myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.Int32");
myColumn.AllowDBNull = false;
myColumn.Caption = "intUserTypeID";
myColumn.ColumnName = "intUserTypeID";
myColumn.DefaultValue = 1;
DataColumn myColumndtAddedOn = new DataColumn();
myColumndtAddedOn.DataType = System.Type.GetType("System.DateTime");
myColumndtAddedOn.AllowDBNull = false;
myColumndtAddedOn.Caption = "dtAddedOn";
myColumndtAddedOn.ColumnName = "dtAddedOn";
myColumndtAddedOn.DefaultValue = DateTime.Now;
dtInsrt.Columns.Add(myColumn);
dtInsrt.Columns.Add(myColumndtAddedOn);
for (int i = 0; i < csvdt.Rows.Count; i++)
{
CryptoGraphy objEncrypt = new CryptoGraphy();
dtInsrt.ImportRow(csvdt.Rows[i]);
dtInsrt.Rows[i]["Email"] = Convert.ToString(dtInsrt.Rows[i]["Email"]).Replace("\r", null);
dtInsrt.Rows[i]["vchrPassword"] = objEncrypt.Encrypt(Convert.ToString(csvdt.Rows[i]["vchrPassword"]));
}
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
using (SqlBulkCopy objbulk = new SqlBulkCopy(conn))
{
//assigning Destination table name
objbulk.DestinationTableName = "Scrl_RegistrationTbl";
//Mapping Table column
objbulk.ColumnMappings.Add("FirstName", "vchrFirstName");
objbulk.ColumnMappings.Add("LastName", "vchrLastName");
objbulk.ColumnMappings.Add("Email", "vchrUserName");
objbulk.ColumnMappings.Add("vchrActive", "vchrActive");
objbulk.ColumnMappings.Add("vchrPassword", "vchrPassword");
objbulk.ColumnMappings.Add("intUserTypeID", "intUserTypeID");
objbulk.ColumnMappings.Add("dtAddedOn", "dtAddedOn");
//dtAddedOn
//Inserting Datatable Records to DataBase
conn.Open();
//transaction.Commit();
//IsSuccessSave = true;
objbulk.WriteToServer(dtInsrt);
conn.Close();
for (int i = 0; i < csvdt.Rows.Count; i++)
{
SendMail(csvdt.Rows[i]["Email"].ToString().Replace("\r", null), csvdt.Rows[i]["vchrPassword"].ToString(), csvdt.Rows[i]["FirstName"].ToString() + " " + csvdt.Rows[i]["LastName"].ToString());
DataTable dtRecord = new DataTable();
dtRecord.Clear();
objdoreg.UserName = csvdt.Rows[i]["Email"].ToString().Replace("\r", null);
dtRecord = objdareg.GetDataTableRecord(objdoreg, DA_Registrationdetails.RegistrationDetails.UserRecordByMail);
//string id = "id=" + dtRecord.Rows[0]["intRegistrationId"].ToString();
//dt = objLoginDB.GetDataTable(objLogin, DA_SKORKEL.DA_Login.Login_1.UserDetails);
if (ISAPIURLACCESSED == "1")
{
StringBuilder UserURL = new StringBuilder();
UserURL.Append(APIURL);
UserURL.Append("registerUser.action?");
UserURL.Append("uid=");
UserURL.Append(Convert.ToString(dtRecord.Rows[0]["intRegistrationId"]));
UserURL.Append("&userId=");
UserURL.Append(Convert.ToString(dtRecord.Rows[0]["vchrUserName"]));
UserURL.Append("&password=");
UserURL.Append(Convert.ToString(dtRecord.Rows[0]["vchrPassword"]));
UserURL.Append("&firstName=");
UserURL.Append(Convert.ToString(dtRecord.Rows[0]["vchrFirstName"]));
UserURL.Append("&lastName=");
UserURL.Append(Convert.ToString(dtRecord.Rows[0]["vchrLastName"]));
UserURL.Append("&userType=STUDENT");
UserURL.Append("&userContextIds=" + null);
UserURL.Append("&friendUserIds=" + null);
UserURL.Append("&lawRelated=" + null);
try
{
HttpWebRequest myRequest1 = (HttpWebRequest)WebRequest.Create(UserURL.ToString());
myRequest1.Method = "GET";
WebResponse myResponse1 = myRequest1.GetResponse();
StreamReader sr = new StreamReader(myResponse1.GetResponseStream(), System.Text.Encoding.UTF8);
String result = sr.ReadToEnd();
objAPILogDO.strURL = UserURL.ToString();
objAPILogDO.strAPIType = "Student";
objAPILogDO.strResponse = result;
if (ip == null)
objAPILogDO.strIPAddress = Request.ServerVariables["REMOTE_ADDR"];
objAPILogDA.AddEditDel_Scrl_APILogDetailsTbl(objAPILogDO, DA_Scrl_APILogDetailsTbl.Scrl_APILogDetailsTbl.Insert);
}
catch (Exception ex)
{ ex.Message.ToString(); }
}
}
lblError.Visible = true;
lblError.Text = "User's list uploaded successfully.";
ScriptManager.RegisterStartupScript(this, this.GetType(), "starScripts1", "hideLoader1();", true);
}
}
//Sending Emails
private void SendMail(string UsersMail, string UsersPwd, string UserName)
{
try
{
//DataTable dtRecord = new DataTable();
string body = null;
string MailURL = ConfigurationManager.AppSettings["MailURL"];
string MailTo = UsersMail;
string MailURLFinal = MailURL + "/Landing.aspx";
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", UserName);
body = body.Replace("{Content}", "<span style='font-size:14px;color:#373A36;'>Your email: " + UsersMail +
"</span> and password: " + UsersPwd +
"<span style='font-weight:bold;font-size:14px;color:#373A36;'></span><br/> Please login and reset password on first login. <br /><br/>Login URL:" +
" <a href='" + MailURLFinal + "' style ='background: #01B7BD;padding: 5px 10px; border-radius: 15px;color: #fff;text-decoration: none;'>Login</a>");
string subject = "Welcome, " + UserName + " - Login Details";
EmailSender.SendEmail(MailTo, subject, body);
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}