Example #1
0
        public ActionResult CreatePuz()
        {
            Session["LecID"] = Session["LecID"];

            var model = new CreatePuz
            {
            };

            return(View(model));
        }
Example #2
0
        public ActionResult CreatePuz(string action, CreatePuz cl)
        {
            Session["LecID"] = Session["LecID"];
            String LecID = Session["LecID"].ToString();

            LecID = LecID.ToUpper();
            if (action.Equals("home"))
            {
                return(RedirectToAction("Index"));
            }

            else if (action.Equals("back"))
            {
                return(RedirectToAction("ManagePuzzleLec"));
            }


            else if (action.Equals("create"))
            {
                if (ModelState.IsValid)
                {
                    string[] Answer      = cl.Answer.Split('\n');
                    string[] Question    = cl.Question.Split('\n');
                    string   PuzzleName  = cl.PuzzleName;
                    int      PuzzleScore = cl.PuzzleScore;
                    int      PuzzleHint  = cl.PuzzleHint;

                    int    counter;
                    int    tempcounter;
                    String AnswerID;
                    String QuestionID;


                    string connectionString = @"Data Source=msi;Initial Catalog=SEFASSIGNMENT;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                    System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);

                    sqlConnection.Open();

                    System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM [SEFASSIGNMENT].[dbo].[Puzzle]");
                    sqlCommand.Connection = sqlConnection;
                    int    count    = Convert.ToInt32(sqlCommand.ExecuteScalar()) + 1;
                    String PuzzleID = "PZ" + count.ToString("000");

                    sqlCommand = new System.Data.SqlClient.SqlCommand("INSERT INTO [SEFASSIGNMENT].[dbo].[Puzzle] (\"Puzzle_ID\", \"Puzzle_Name\", Puzzle_Score, Hint_Score,\"LEC_ID\") VALUES('" + PuzzleID + "','" + PuzzleName + "'," + PuzzleScore + "," + PuzzleHint + ",'" + LecID + "')");
                    //return Content("INSERT INTO [SEFASSIGNMENT].[dbo].[Puzzle] (\"Puzzle_ID\", \"Puzzle_Name\", Puzzle_Score, Hint_Score,\"LEC_ID\") VALUES('" + PuzzleID + "','" + PuzzleName + "'," + PuzzleScore + "," + PuzzleHint + ",'" + LecID + "')");
                    sqlCommand.Connection = sqlConnection;
                    count = Convert.ToInt32(sqlCommand.ExecuteScalar());


                    for (int i = 0; i < Answer.Length; i++)
                    {
                        sqlCommand            = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM [SEFASSIGNMENT].[dbo].[Answer]");
                        sqlCommand.Connection = sqlConnection;
                        counter  = Convert.ToInt32(sqlCommand.ExecuteScalar()) + 1;
                        AnswerID = "ANS" + counter.ToString("000");

                        sqlCommand            = new System.Data.SqlClient.SqlCommand("INSERT INTO [SEFASSIGNMENT].[dbo].[Answer] (\"Answer_ID\", \"Answer_Content\", \"Puzzle_ID\") VALUES('" + AnswerID + "','" + Answer[i] + "','" + PuzzleID + "')");
                        sqlCommand.Connection = sqlConnection;
                        tempcounter           = Convert.ToInt32(sqlCommand.ExecuteScalar());
                    }

                    for (int i = 0; i < Question.Length; i++)
                    {
                        sqlCommand            = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM [SEFASSIGNMENT].[dbo].[Question]");
                        sqlCommand.Connection = sqlConnection;
                        counter    = Convert.ToInt16(sqlCommand.ExecuteScalar()) + 1;
                        QuestionID = "QS" + counter.ToString("000");

                        sqlCommand            = new System.Data.SqlClient.SqlCommand("INSERT INTO [SEFASSIGNMENT].[dbo].[Question] (\"Question_ID\", \"Question_Content\", \"Puzzle_ID\") VALUES('" + QuestionID + "','" + Question[i] + "','" + PuzzleID + "')");
                        sqlCommand.Connection = sqlConnection;
                        tempcounter           = Convert.ToInt32(sqlCommand.ExecuteScalar());
                    }
                    TempData["AlertMessage"] = "Successfully created puzzle! Returning to main menu.";
                    return(RedirectToAction("HomeLec"));
                }

                else
                {
                    return(View("CreatePuz", cl));
                }
            }


            return(View("CreatePuz", cl));
        }