C#

Features ›› Game ›› getQuestionScores ›› Sample Code ››
Parent Previous Next

Get Question Scores


1. getQuestionScores.cs


These four packages are required to be used.



Step 1. Create class file to store content of the web response.




2. getQuestionScores.aspx.cs


These 15 packages are required to be used.




Code in Common

(1). Check if user id is inside session. When the user id is inside user session, it will automatically populate into the user id textbox, so that the user need not manually type his user id repeatedly.




Step 2. Store details entered by the user. If user id exist in session, user need not enter again (refer to common stuff).




Step 3. Create URL which will be post to the server. User inputs need to be stored in the class created, and serialize into JSON format. In this function, both header and content are needed.



Step 4. Post to the server, and store response. URL is sent using web request function, and a response will be send back from the server. The response need to be deserialized into the ‘getQuestionScores’ class.




Step 5. Obtain error code. If error code is ‘010000’ which means invocation successful, populate attributes of the ‘getQuestionScores’ class into the Grid View and Label. Else, we will display the ErrorText and ErrorDetails.




Overview of getQuestionScores.cs


using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace Demo

{

   public class getQuestionScoresRootObject

   {

       public getQuestionScoresContent Content { get; set; }

   }


   public class getQuestionScoresContent

   {

       public getQuestionScoresServiceResponse ServiceResponse { get; set; }

   }


   public class getQuestionScoresServiceResponse

   {

       public getQuestionScoresScoreDetails ScoreDetails { get; set; }

       public getQuestionScoresServiceRespHeader ServiceRespHeader { get; set; }

   }


   public class getQuestionScoresServiceRespHeader

   {

       public string ErrorText { get; set; }

       public object ErrorDetails { get; set; }

       public string GlobalErrorID { get; set; }

   }


   public class getQuestionScoresScoreDetails

   {

       public getQuestionScoresQuestionScores QuestionScores { get; set; }

   }


   public class getQuestionScoresQuestionScore

   {

       public string score { get; set; }

       public string question_Id { get; set; }

   }


   public class getQuestionScoresQuestionScores

   {

       [JsonConverter(typeof(ArrayConverter<getQuestionScoresQuestionScore>))]

       public List<getQuestionScoresQuestionScore> QuestionScore { get; set; }

   }

}


Download



Overview of getQuestionScores.aspx.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using DotNetOpenAuth.OAuth2;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using System.Collections.Specialized;

using System.Configuration;

using System.Net;

using System.IO;

using System.Windows.Forms;

using System.Data;


namespace Demo

{

   public partial class GetQuestionScores : System.Web.UI.Page

   {

       protected void Page_Load(object sender, EventArgs e)

       {

           if (Session["userid"] != null)

           {

               txtUserID.Text = (string)Session["userid"];

           }

       }


       protected void btnSend_Click(object sender, EventArgs e)

       {

           if (Page.IsValid)

           {

               gvGetQuestionScore.DataSource = null;

               gvGetQuestionScore.DataBind();


               try

               {

                   string userID = txtUserID.Text;

                   Session["userid"] = userID;


                   string gameID = txtGameID.Text;

                   string start = txtStart.Text;

                   string end = txtEnd.Text;

                   string mode = txtMode.Text;                  


                   var header = new HeaderJson();

                   header.userID = userID;

                   header.PIN = "";

                   header.serviceName = "getQuestionScores";

                   header.OTP = "";

                   var headerJson = new HeaderJsonObject();

                   headerJson.Header = header;

                   string strHeader = JsonConvert.SerializeObject(headerJson);


                   DateTime dStart = Convert.ToDateTime(start);

                   DateTime dend = Convert.ToDateTime(end);


                   var content = new ContentJson();

                   content.gameID = gameID;

                   content.start = dStart;

                   content.end = dend;

                   content.mode = mode;

                   var contentJson = new ContentJsonObject();

                   contentJson.Content = content;

                   string strContent = JsonConvert.SerializeObject(contentJson);


                   string url = "http://tbankonline.com/SMUtBank_API/Gateway?Header=" + strHeader + "&Content=" + strContent;


                   var webRequest = (HttpWebRequest)WebRequest.Create(url);

                   webRequest.ContentType = "application/json";

                   webRequest.Method = "POST";


                   var result = "";

                   var httpResponse = (HttpWebResponse)webRequest.GetResponse();

                   using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

                   {

                       result = streamReader.ReadToEnd();

                   }


                   getQuestionScoresRootObject score = new getQuestionScoresRootObject();

                   score = JsonConvert.DeserializeObject<getQuestionScoresRootObject>(result);


                   string globalErrorID = score.Content.ServiceResponse.ServiceRespHeader.GlobalErrorID.ToString();


                   if (globalErrorID == "010000")

                   {

                       int counter = score.Content.ServiceResponse.ScoreDetails.QuestionScores.QuestionScore.Count();

                       DataTable dt = new DataTable();

                       DataColumn col1 = new DataColumn("  ", typeof(string));

                       DataColumn col2 = new DataColumn("Score", typeof(string));

                       DataColumn col3 = new DataColumn("QuestionID", typeof(string));

                       dt.Columns.Add(col1);

                       dt.Columns.Add(col2);

                       dt.Columns.Add(col3);


                       // put data into the data table

                       for (int i = 0; i <= counter - 1; i++)

                       {

                           DataRow row = dt.NewRow();

                           dt.Rows.Add(row);

                           dt.Rows[i][col1] = i + 1;

                           dt.Rows[i][col2] = score.Content.ServiceResponse.ScoreDetails.QuestionScores.QuestionScore[i].score?.ToString() ?? " ";

                           dt.Rows[i][col3] = score.Content.ServiceResponse.ScoreDetails.QuestionScores.QuestionScore[i].question_Id?.ToString() ?? " ";

                       }

                       gvGetQuestionScore.DataSource = dt;

                       gvGetQuestionScore.DataBind();

                   }

                   else

                   {

                       lblTest.Text = score.Content.ServiceResponse.ServiceRespHeader.ErrorText.ToString();

                       string errorMessage = score.Content.ServiceResponse.ServiceRespHeader.ErrorDetails.ToString();

                       lblErrorMessage.Text = errorMessage;

                   }

               }

               catch (Exception ex)

               {

                   lblExceptionMsg.Text = ex.ToString();

               }

           }

       }

   }

}


Download

Created with the Personal Edition of HelpNDoc: Free Web Help generator