-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDetails.aspx.cs
276 lines (229 loc) · 10.9 KB
/
TestDetails.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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Net;
public partial class TestDetails : System.Web.UI.Page
{
DAL dal = new DAL();
DataSet obDs = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userid"] != null)
{
if(Session["TestDetails"] != null)
{
DataTable dtanswerskeys = new DataTable();
//Add Columns to datatable
dtanswerskeys.Columns.Add("QuestionNo", typeof(Int32));
dtanswerskeys.Columns.Add("Attempted", typeof(string));
dtanswerskeys.Columns.Add("Correct", typeof(string));
divtable.InnerHtml = "";
StringBuilder htmlTable = new StringBuilder();
DataTable dt = (DataTable)Session["TestDetails"];
if (dt.Rows.Count > 0)
{
htmlTable.Append("");
htmlTable.Append("<table class='table table-xs mb-0' > ");
htmlTable.Append("<tr>");
htmlTable.Append("<td><b>Question No.</b></td> <td> <b>Attempted Option</b></td> <td><b>Correct Option</b></td>");
htmlTable.Append("</tr>");
int TotalQuestions = dt.Rows.Count;
int CorrectAnsCount = 0;
int AttemptedQuestionsCount = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
string QuestionNo = string.Empty;
string Attemptedoption = string.Empty;
string CorrectOption = string.Empty;
QuestionNo = dt.Rows[i]["Row_Number"].ToString();
ParameterCollection obParam = new ParameterCollection();
obParam.Add("@qid",dt.Rows[i]["qid"].ToString());
obDs = dal.fnRetriveByPro("GetAnwersByQidforScore", obParam);
if (obDs.Tables.Count > 0)
{
if (obDs.Tables[0].Rows.Count > 0)
{
for(int j=0;j< obDs.Tables[0].Rows.Count; j++)
{
if(Convert.ToInt32(obDs.Tables[0].Rows[j]["ansid"])== Convert.ToInt32(dt.Rows[i]["CorrectAnsid"]))
{
CorrectOption = obDs.Tables[0].Rows[j]["ansdescription"].ToString();
}
if (Convert.ToInt32(dt.Rows[i]["Ansid"]) != 0)
{
if (Convert.ToInt32(obDs.Tables[0].Rows[j]["ansid"]) == Convert.ToInt32(dt.Rows[i]["Ansid"]))
{
Attemptedoption = obDs.Tables[0].Rows[j]["ansdescription"].ToString();
}
}else
{
Attemptedoption = "Not Attempted";
}
}
}
}
if (dt.Rows[i]["Ansid"].ToString() == dt.Rows[i]["CorrectAnsid"].ToString())
{
CorrectAnsCount = CorrectAnsCount + 1;
}
if (Convert.ToBoolean(dt.Rows[i]["IsAttempted"]))
{
AttemptedQuestionsCount = AttemptedQuestionsCount + 1;
}
dtanswerskeys.Rows.Add(Convert.ToInt32(QuestionNo), Attemptedoption, CorrectOption);
htmlTable.Append("<tr>");
htmlTable.Append("<td>"+QuestionNo+"</td> <td> "+Attemptedoption+" </td> <td> "+CorrectOption+" </td>");
htmlTable.Append("</tr>");
}
Session["dtanwerkeys"] = dtanswerskeys;
dal.fnExecuteNonQuery("insert into MyScoreCard ([Userid],[Testid],[TotalQuestions],[TotalAttempted],[TotalCorrected]) values ('" + dt.Rows[0]["Userid"].ToString() + "','"+ dt.Rows[0]["Testid"].ToString() + "','"+ TotalQuestions.ToString()+ "','"+ AttemptedQuestionsCount.ToString()+ "','"+ CorrectAnsCount.ToString()+ "')");
//lblrightquestions.Text = "Total Questions: " + TotalQuestions + " Attempted : " + AttemptedQuestionsCount + " Correct Questions :" + CorrectAnsCount;
htmlTable.Append("</table>");
divtable.InnerHtml = htmlTable.ToString();
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "ResultPopup();", true);
}
Session["TestDetails"] = null;
}
string userid = Session["userid"].ToString().ToUpper();
string username = Session["username"].ToString().ToUpper();
if (!IsPostBack)
{
BindAllTestByUser(userid);
}
}
else
{
Response.Redirect("Login");
}
}
public void BindAllTestByUser(string userid)
{
ParameterCollection obParam = new ParameterCollection();
obParam.Add("@userid", userid);
//obDs = dal.fnRetriveByPro("BindAlltestByuser", obParam); // old
obDs = dal.fnRetriveByPro("BindAlltest", obParam); //new
if (obDs.Tables.Count > 0)
{
DataTable dt = obDs.Tables[0];
if (dt.Rows.Count > 0)
{
Datalist1.DataSource = dt;
//Datalist1.DataSource = Get(userid);
Datalist1.DataBind();
}
}
}
public List<TestInfo> Get(string userid)
{
List<TestInfo> tests = new List<TestInfo>();
using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "execute BindAlltestByuser '"+ userid + "'";
sqlcmd.Connection = sqlcon;
sqlcon.Open();
SqlDataReader sqlrd = sqlcmd.ExecuteReader();
while (sqlrd.Read())
{
tests.Add(new TestInfo() { testid = sqlrd["testid"].ToString(), testname = sqlrd["testname"].ToString(), testdescription = sqlrd["testdescription"].ToString(), testtotalquestions = sqlrd["totalquestions"].ToString(), testduration =sqlrd["testduration"].ToString(), testmarks= sqlrd["totalmarks"].ToString() });
}
sqlcon.Close();
}
return tests;
}
protected void Datalist1_EditCommand(object source, DataListCommandEventArgs e)
{
int index = e.Item.ItemIndex;
string testid = ((Label )Datalist1.Items[index].FindControl("lbltestid")).Text;
string testtext = ((Button)Datalist1.Items[index].FindControl("btntest")).Text;
if(testtext.ToUpper().Trim()== "PAID")
{
Response.Redirect("OnlinePayment");
}else
{
Server.Transfer("StartTest.aspx?tid=" + HttpUtility.UrlEncode(Encrypt(testid)) + "");
}
}
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
public void EpsionPrint(DataTable dt)
{
if (dt.Rows.Count > 0)
{
try
{
Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
Paragraph Text = new Paragraph(" Result With Anwer Keys");
Paragraph Text2 = new Paragraph(" ");
Paragraph Text3 = new Paragraph(" ");
pdfDoc.Add(Text);
pdfDoc.Add(Text2);
pdfDoc.Add(Text3);
PdfPTable table = new PdfPTable(3);
table.AddCell("Question No");
table.AddCell("Attempted");
table.AddCell("Correct");
for (int i = 0; i < dt.Rows.Count; i++)
{
table.AddCell(dt.Rows[i]["QuestionNo"].ToString());
table.AddCell(dt.Rows[i]["Attempted"].ToString());
table.AddCell(dt.Rows[i]["Correct"].ToString());
}
pdfDoc.Add(table);
pdfWriter.CloseStream = false;
pdfDoc.Close();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ScoreCard.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
catch (Exception ex)
{ Response.Write(ex.Message); }
}
}
protected void btndownloadscorecard_Click(object sender, EventArgs e)
{try
{
DataTable dt = (DataTable)Session["dtanwerkeys"];
EpsionPrint(dt);
}
catch(Exception ex)
{
}
}
}