This repository was archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQfunctions.cs
More file actions
456 lines (399 loc) · 16.7 KB
/
Qfunctions.cs
File metadata and controls
456 lines (399 loc) · 16.7 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Web;
namespace Qbert
{
public class Qfunctions
{
public void setExpectHeader()
{
System.Net.ServicePointManager.Expect100Continue = false; //line added to fix proxy server problem
}
public void loadgrid(OpenFileDialog ofd, DataTable dt)
{
FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
StreamReader sr = new StreamReader(fs);
dt.TableName = "LeadData";
string[] strArray;
string strLine;
char[] charArray = new char[] { ',' };
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);
for (int x = 0; x <= strArray.GetUpperBound(0); x++)
{
dt.Columns.Add(strArray[x].Trim());
}
strLine = sr.ReadLine();
while (strLine != null)
{
strArray = strLine.Split(charArray);
DataRow dr = dt.NewRow();
for (int i = 0; i <= strArray.GetUpperBound(0); i++)
{
dr[i] = strArray[i].Trim();
}
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
}
public void FixPhone(DataGridView dgv)
{
foreach (DataGridViewColumn dc in dgv.Columns)
{
if (dc.HeaderText == "Phone")
{
int dcindex = dc.Index;
int drcount = dgv.RowCount;
for (int i = 0; i < drcount; i++)
{
string oldphone = (String)dgv[dcindex, i].Value;
string newphone = formatphone(oldphone);
dgv[dcindex, i].Value = newphone;
}
}
}
}
private string formatphone(string sNumToBeFormatted) //case statement for phone formatting
{
if (sNumToBeFormatted == null)
{
return sNumToBeFormatted;
}
else
{
string functionReturnValue = null; //string to hand back to the form
int iNumberLength = 0; // the length of the string to be evaluated
sNumToBeFormatted.Trim(); //trim the fat of the string (ie blanks on the end)
iNumberLength = sNumToBeFormatted.Length; //set the length of the string
switch (iNumberLength) //each case of this switch is a function to perform on the string based on the length of the string
{
case 7:
//Format : #######
functionReturnValue = sNumToBeFormatted.Substring(0, 3) + "-" + sNumToBeFormatted.Substring(3);
return functionReturnValue;
case 8:
//Format : ###-#### or ### ####
if (sNumToBeFormatted.Substring(3, 1) == "-")
{
functionReturnValue = sNumToBeFormatted;
return functionReturnValue;
}
else
{
functionReturnValue = sNumToBeFormatted.Substring(0, 3) + "-" + sNumToBeFormatted.Substring(4);
return functionReturnValue;
}
case 10:
//Format : ##########
functionReturnValue = "(" + sNumToBeFormatted.Substring(0, 3) + ") " + sNumToBeFormatted.Substring(3, 3) + "-" + sNumToBeFormatted.Substring(6);
return functionReturnValue;
case 11:
//Format ######-#### or ###-#######
if (sNumToBeFormatted.Substring(6, 1) == "-")
{
functionReturnValue = "(" + sNumToBeFormatted.Substring(0, 3) + ") " + sNumToBeFormatted.Substring(3);
return functionReturnValue;
}
else
{
functionReturnValue = "(" + sNumToBeFormatted.Substring(0, 3) + ") " + sNumToBeFormatted.Substring(4, 3) + "-" + sNumToBeFormatted.Substring(7);
return functionReturnValue;
}
case 12:
//Format : ### ###-####
functionReturnValue = "(" + sNumToBeFormatted.Substring(0, 3) + ") " + sNumToBeFormatted.Substring(4, 3) + "-" + sNumToBeFormatted.Substring(8);
return functionReturnValue;
case 13:
//Format : (###)###-####
functionReturnValue = sNumToBeFormatted.Substring(0, 5) + " " + sNumToBeFormatted.Substring(5);
return functionReturnValue;
default:
//Return Value Passed
functionReturnValue = sNumToBeFormatted;
return functionReturnValue;
}
}
}
private string vlookup(string sourcevalue, string lookupcolumn, string returncolumn, DataTable maptable)
{
string returnvalue = "";
foreach (DataRow maprow in maptable.Rows)
{
if (maprow[lookupcolumn].ToString() == sourcevalue && sourcevalue != "" && (maprow[returncolumn] != null))
{
returnvalue = maprow[returncolumn].ToString();
return returnvalue;
}
}
return sourcevalue;
}
public void FindDupes(DataGridView dataGridView1, DataSet ds)
{
if (!ds.Tables[0].Columns.Contains("Duplicate"))
{
//ds.Tables[0].Columns.Add("Duplicate", "Duplicate");
DataColumn dc = ds.Tables[0].Columns.Add("Duplicate");
dc.ColumnName = "Duplicate";
}
foreach (DataGridViewRow dgvrow in dataGridView1.Rows)
{
if (dataGridView1["Email", dgvrow.Index].Value != null)
{
string email = dataGridView1["Email", dgvrow.Index].Value.ToString();
int a = 0;
if (email != "")
{
while (a < dataGridView1.RowCount)
{
if (dgvrow.Index != a && dataGridView1["Email", a].Value != null && email == dataGridView1["Email", a].Value.ToString())
{
dataGridView1["Duplicate", dgvrow.Index].Value = "TRUE";
}
a++;
}
}
}
}
}
public void FixZip(DataGridView dgv)
{
foreach (DataGridViewColumn dc in dgv.Columns)
{
if (dc.HeaderText == "Zip/Postal Code")
{
int dcindex = dc.Index;
int drcount = dgv.RowCount;
for (int i = 0; i < drcount; i++)
{
string oldzip = (String)dgv[dcindex, i].Value;
string newzip = formatzip(oldzip);
dgv[dcindex, i].Value = newzip;
}
}
}
}
private string formatzip(string oldzip)
{
oldzip.Trim();
oldzip.Substring(0, 4);
return oldzip;
}
public DataTable loadcsv(OpenFileDialog ofd)
{
FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
StreamReader sr = new StreamReader(fs);
TextReader tx = sr;
CsvParser csvp = new CsvParser();
DataTable dt = csvp.Parse(tx, true);
return dt;
}
public void FixFieldLengths(DataGridView dgv)
{
foreach (DataGridViewColumn dc in dgv.Columns)
{
switch (dc.Name)
{
case "FirstName":
fixlength(dc, 20);
break;
case "LastName":
fixlength(dc, 40);
break;
case "StateProvince":
fixlength(dc, 20);
break;
case "Zip":
fixlength(dc, 20);
break;
case "Title":
fixlength(dc, 80);
break;
case "Website":
fixlength(dc, 255);
break;
}
}
}
private void fixlength(DataGridViewColumn dc, int lim)
{
DataGridView dgv = dc.DataGridView;
int dcindex = dc.Index;
int drcount = dgv.RowCount;
for (int i = 0; i < drcount; i++)
{
string oldval = (String)dgv[dcindex, i].Value;
if (oldval != null && !(oldval.Length < lim))
{
oldval.Trim();
string newval = oldval.Substring(0, lim);
dgv[dcindex, i].Value = newval;
}
}
}
public void RemoveDupes(DataGridView dataGridView1, DataSet ds)
{
int emailrow = 0;
foreach (DataGridViewRow dgvrow in dataGridView1.Rows)
{
if (dataGridView1["Email", dgvrow.Index].Value != null && dataGridView1["Email", dgvrow.Index].Value.ToString() != "")
{
string email = dataGridView1["Email", dgvrow.Index].Value.ToString();
int a = emailrow;
while (a < dataGridView1.RowCount)
{
if (dgvrow.Index != a && dataGridView1["Email", a].Value != null && email == dataGridView1["Email", a].Value.ToString())
{
dataGridView1.Rows.Remove(dataGridView1.Rows[a]);
}
a++;
}
}
emailrow++;
}
ds.Tables[0].Columns.Remove("Duplicate");
}
public void PostToEloqua(DataSet ds, Dictionary<string, string> elqvals)
{
ProgressInd progress = ShowProgress(0, ds.Tables[0].Rows.Count, 0); //initialize progress bar
progress.Show();
string eloquaposturl = elqvals["Eloqua Post URL"].Trim(); //set eloqua url
elqvals.Remove("Eloqua Post URL");
string elqvars = "";
foreach (KeyValuePair<string, string> entry in elqvals) //string together eloqua and campaign static values
{
string field = HttpUtility.UrlEncode(entry.Key.ToString());
string value = HttpUtility.UrlEncode(entry.Value.ToString());
elqvars += entry.Key.ToString() + "=" + entry.Value.ToString() + "&";
}
DataTable dt = ds.Tables[0];
dt.Columns.Add("Qbert Post Result");
foreach (DataRow dr in dt.Rows) //submit each row to eloqua
{
ShowProgress(progress, 0);
if (!progress.cancelled) //when the progress indicator closes, the value of .text is ""
{
string rowdata = "";
foreach (DataColumn dc in dt.Columns)
{
if (dr[dc].ToString() != null && dr[dc].ToString() != "")
{
rowdata += HttpUtility.UrlEncode(dc.ColumnName) + "=" + HttpUtility.UrlEncode(dr[dc].ToString()) + "&";
}
rowdata.Trim();
}
try
{
string result = eloquapost(eloquaposturl, elqvars + rowdata); //call function to send data to eloqua
dr["Qbert Post Result"] = result;
}
catch (Exception er)
{
string errorstring = er.ToString();
QError oops = new QError(errorstring);
oops.ShowDialog();
}
ShowProgress(progress, 1); //increment progress bar
}
else
{
break;
}
}
progress.button1.Text = "OK";
}
private static string eloquapost(string url, string data)
{
string vystup = null;
try
{
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes(data);
//Initialisation, we use localhost, change if appliable
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
//WebReq.Expect = "False";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
//Console.WriteLine(WebResp.StatusCode);
//Console.WriteLine(WebResp.Server);
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
vystup = "Status Code: " + WebResp.StatusCode.ToString() + "; Status Description: " + WebResp.StatusDescription.ToString();
WebResp.Close();
Answer.Close();
//Congratulations, you just requested your first POST page, you
//can now start logging into most login forms, with your application
//Or other examples.
}
catch (Exception ex)
{
vystup += "; " + ex.Message.ToString();
return vystup;
}
return vystup.Trim() + "\n";
}
public ProgressInd ShowProgress(int min, int max, int val)
{
ProgressInd progress = new ProgressInd();
progress.progBar1.Minimum = min;
progress.progBar1.Maximum = max;
progress.progBar1.Value = val;
return progress;
} //create progress indicator
public void ShowProgress(ProgressInd progress, int step)
{
if (progress.Text != "Cancelled")
{
progress.progBar1.Value += step; //increment progress bar
progress.Text = progress.progBar1.Value.ToString() + " of " + progress.progBar1.Maximum.ToString() + " items processed...";
progress.Refresh();
Application.DoEvents();
}
}
public void RemoveBlankRows(DataSet ds)
{
for (int rows = ds.Tables[0].Rows.Count - 1; rows > 0; rows--)
{
bool hasdata = false;
foreach (DataColumn dc in ds.Tables[0].Columns)
{
if (ds.Tables[0].Rows[rows][dc].ToString() != "")
{
hasdata = true;
break;
}
}
if (!hasdata)
{
ds.Tables[0].Rows[rows].Delete();
}
}
}
public void InsertBlankColumn(DataSet ds)
{
InsertColumn ic = new InsertColumn();
ic.ShowDialog();
}
}
}