-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.cs
303 lines (284 loc) · 12.4 KB
/
Common.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace iIECaB
{
public class Common
{
private static string DBName;
private static string ServerName;
private static string uName;
private static string pwd;
internal static string GetConnectionString(string UserName, string Password)
{
return "Server=" + ServerName + ";database=" + DBName + ";uid=" + uName + ";pwd=" + pwd + ";";
}
static Common()
{
try
{
DBName = ConfigurationManager.AppSettings["DBName"];
ServerName = ConfigurationManager.AppSettings["DBServerName"];
uName = ConfigurationManager.AppSettings["DBUserName"];
pwd = ConfigurationManager.AppSettings["DBPassword"];
}
catch (Exception ex)
{
}
}
internal static UserContext Authenticate(string UserName, string Password)
{
// Change History
//
// Date Edit Author Comment
// -----------+-------+-------+---------------------------------------------
// 02-Feb-12 [100] SSN Created
// -----------+-------+-------+---------------------------------------------
UserContext userInfo = new UserContext();
userInfo.Authenticated = false;
if (UserName.Trim() != "" && Password.Trim() != "")
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = GetConnectionString(UserName, Password);
con.Open();
if (con.State == ConnectionState.Open)
{
using (SqlCommand cmd = con.CreateCommand())
{
//cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "select users_refno from users where username='" + UserName + "' and password='" + Password + "'";
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
// return userInfo;
dr.Read();
userInfo.Authenticated = true;
userInfo.userName = UserName;
userInfo.UserRefno = ((int)dr.GetValue(dr.GetOrdinal("users_REFNO")));
}
}
}
}
con.Close();
}
catch (Exception ex)
{
userInfo.Authenticated = false;
}
}
else
{
userInfo.Authenticated = false;
}
return userInfo;
}
internal static Dictionary<string,string> GetQuesAndAns(int level_refno,int event_refno)
{
Dictionary<string, string> dictResults = new Dictionary<string, string>();
List<Dictionary<string, object>> results = new List<Dictionary<string, object>>();
Dictionary<string, object> Parameters = new Dictionary<string, object>();
Parameters.Add("@level_refno", Convert.ToInt32(level_refno));
Parameters.Add("@event_refno", Convert.ToInt32(event_refno));
results = GetResultsFromSp("sp_GetQuestionAndAnswer", Parameters);
foreach (Dictionary<string, object> Row in results)
{
dictResults.Add((string)Row["QUESTIONS"], (string)Row["ANSWERS"]);
}
return dictResults;
}
internal static int GetUserLevel(int user_refno,int event_refno)
{
int userLevel;
userLevel = 0;
List<Dictionary<string, object>> userLevels = new List<Dictionary<string, object>>();
Dictionary<string, object> Parameters = new Dictionary<string, object>();
Parameters.Add("@USER_REFNO", Convert.ToInt32(user_refno));
Parameters.Add("@event_refno", Convert.ToInt32(event_refno));
userLevels = GetResultsFromSp("sp_GetUserLevel", Parameters);
foreach (Dictionary<string, object> Row in userLevels)
{
userLevel = (Int32)Row["LEVEL_REFNO"];
}
return userLevel;
}
internal static List<Dictionary<string, Object>> GetResultsFromSp(string SPName, Dictionary<string, object> Parameters)
{
// Change History
//
// Date Edit Author Comment
// -----------+-------+-------+---------------------------------------------
// 02-Feb-12 [100] SSN Created
// -----------+-------+-------+---------------------------------------------
SqlDataReader reader;
reader = null;
List<Dictionary<string, object>> results = new List<Dictionary<string, object>>();
Dictionary<string, Object> ColValue = new Dictionary<string, object>();
try
{
SqlConnection cn = new SqlConnection(GetConnectionString(uName,pwd));
cn.Open();
if (cn.State == ConnectionState.Open)
{
SqlCommand Cmd = cn.CreateCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandText = SPName;
foreach (var item in Parameters)
{
Cmd.Parameters.AddWithValue(item.Key, item.Value);
}
reader = Cmd.ExecuteReader();
results = CheckAndReturnValue(reader);
}
cn.Close();
}
catch (Exception ex)
{
//UserInfo.Authenticated = false;
}
return results;
}
private static List<Dictionary<string, object>> CheckAndReturnValue(SqlDataReader reader)
{
// Change History
//
// Date Edit Author Comment
// -----------+-------+-------+---------------------------------------------
// 02-Feb-12 [100] SSN Created
// -----------+-------+-------+---------------------------------------------
object obj;
int Ordinal = 0;
obj = null;
List<Dictionary<string, object>> results = new List<Dictionary<string, object>>();
Dictionary<string, object> ColValues;
while (reader.Read())
{
ColValues = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
if (reader.GetFieldType(i) == Type.GetType("System.String"))
{
obj = reader.IsDBNull(i) ? String.Empty : reader.GetValue(i);
}
else if (reader.GetFieldType(i) == Type.GetType("System.Decimal"))
{
obj = reader.IsDBNull(i) ? 0 : reader.GetValue(i);
}
else if (reader.GetFieldType(i) == Type.GetType("System.DateTime"))
{
obj = reader.IsDBNull(i) ? new DateTime() : reader.GetDateTime(i);
}
else
{
obj = reader.IsDBNull(Ordinal) ? null : reader.GetValue(i);
}
ColValues.Add(reader.GetName(i).ToUpper(), obj);
}
results.Add(ColValues);
}
return results;
}
internal static int InsertNewLevel(int level,int user_refno,int event_refno)
{
SqlConnection con=new SqlConnection(GetConnectionString(uName,pwd));
con.Open();
if(con.State==ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into userlevels(USERS_REFNO,LEVEL_REFNO,EVENT_REFNO) values(" + user_refno + "," + level + "," + event_refno + ")";
return cmd.ExecuteNonQuery();
}
return 1;
}
internal static int UpdateNewLevel(int level,int user_refno,int event_refno)
{
SqlConnection con = new SqlConnection(GetConnectionString(uName, pwd));
con.Open();
if (con.State == ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "update userlevels set LEVEL_REFNO=" + level + " where users_refno=" + user_refno;
return cmd.ExecuteNonQuery();
}
return 1;
}
internal static string[] GetImagesPath(int nLevel)
{
string[] imgPath=new string[2];
int i;
i = 0;
List<Dictionary<string, object>> imgPaths = new List<Dictionary<string, object>>();
Dictionary<string, object> Parameters = new Dictionary<string, object>();
Parameters.Add("@LEVEL_REFNO", Convert.ToInt32(nLevel));
imgPaths = GetResultsFromSp("sp_GetImagePath", Parameters);
foreach (Dictionary<string, object> Row in imgPaths)
{
imgPath[i]=(String)Row["PATH"];
i++;
}
return imgPath;
}
internal static int CreateNewUser(string userName,string email,string passWord)
{
bool bValidate;
bValidate=ValidateData(userName,"USERNAME");
if(bValidate)
bValidate=ValidateData(email,"EMAILID");
if (bValidate)
{
SqlConnection con = new SqlConnection(GetConnectionString(uName, pwd));
con.Open();
if (con.State == ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into USERS(USERNAME,PASSWORD,EMAILID) values('" + userName + "','" + passWord + "','" + email + "')";
cmd.ExecuteNonQuery();
cmd.CommandText = "select @@identity userrefno";
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
dr.Read();
return (Convert.ToInt32(dr.GetDecimal(0)));
}
}
}
}
return 0;
}
private static bool ValidateData(string value,string fieldName)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = GetConnectionString(uName, pwd);
con.Open();
if (con.State == ConnectionState.Open)
{
using (SqlCommand cmd = con.CreateCommand())
{
//cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "select users_refno from users where " + fieldName + "='" + value + "'";
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
return false;
}
}
}
}
return true;
}
private void CallErrorPage()
{
}
}
}