Tuesday 17 October 2017

How to make KBC game in C# (part-2/5)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace kbc
{
    public partial class QUIZ : Form
    {

        int[] x = new int[15];
        int p = 0;
        string ansfromdb, ansselect;
        returnclass rc = new returnclass();
        int score = 0,qid;
        private string connstring = ConfigurationManager.ConnectionStrings["kbc"].ConnectionString;

        public QUIZ()
        {
            InitializeComponent();
        }

        private void QUIZ_Load(object sender, EventArgs e)
        {
           // label2.Text = startgame.NAME;

            Random r = new Random();
           qid= r.Next(3, 17);
           x[p] = qid;
       

            displayquestion(qid);
         






        }

        private void label3_Click(object sender, EventArgs e)
        {
       
          ansselect = label3.Text;
          if (ansselect.Equals(ansfromdb))
          {
              MessageBox.Show("correct ans!");
              qid++;
              displayquestion(qid);
          l1:
              Random r = new Random();
              int s = r.Next(1, 17);

           bool c= search(x, s);

           if (c==true)
           {
               goto l1;
           }
           else
           {

               p++;
               qid = s;
               x[p] = qid;
               displayquestion(qid);

           }








          }

          else
          {
              MessageBox.Show("game over");


          }





        }

        private void label4_Click(object sender, EventArgs e)
        {
            ansselect = label4.Text;
            if (ansselect.Equals(ansfromdb))
            {
                MessageBox.Show("correct ans!");
                qid++;
                displayquestion(qid);
            l1:
                Random r = new Random();
                int s = r.Next(1, 17);

                bool c = search(x, s);

                if (c == true)
                {
                    goto l1;
                }
                else
                {

                    p++;
                    qid = s;
                    x[p] = qid;
                    displayquestion(qid);

                }








            }

            else
            {
                MessageBox.Show("game over");


            }

        }

        private void label5_Click(object sender, EventArgs e)
        {
            ansselect = label5.Text;
            if (ansselect.Equals(ansfromdb))
            {
                MessageBox.Show("correct ans!");
                qid++;
                displayquestion(qid);
            l1:
                Random r = new Random();
                int s = r.Next(1, 17);

                bool c = search(x, s);

                if (c == true)
                {
                    goto l1;
                }
                else
                {

                    p++;
                    qid = s;
                    x[p] = qid;
                    displayquestion(qid);

                }








            }

            else
            {
                MessageBox.Show("game over");


            }

        }

        private void label6_Click(object sender, EventArgs e)
        {
            ansselect = label6.Text;
            if (ansselect.Equals(ansfromdb))
            {
                MessageBox.Show("correct ans!");
                qid++;
                displayquestion(qid);
            l1:
                Random r = new Random();
                int s = r.Next(1, 17);

                bool c = search(x, s);

                if (c == true)
                {
                    goto l1;
                }
                else
                {

                    p++;
                    qid = s;
                    x[p] = qid;
                    displayquestion(qid);

                }








            }

            else
            {
                MessageBox.Show("game over");


            }



        }


        public void displayquestion(int q_id)
        {

            string sql = "select q_question,q_opA,q_opB,q_opC,q_opD,q_opcORRECT from questions where q_id=" + q_id;
            SqlConnection connection = new SqlConnection(connstring);
            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    label1.Text = reader.GetValue(0).ToString(); //question
                    label3.Text = reader.GetValue(1).ToString(); //opa
                    label4.Text = reader.GetValue(2).ToString();//opb
                    label5.Text = reader.GetValue(3).ToString();//opc
                    label6.Text = reader.GetValue(4).ToString();//opd
                    ansfromdb = reader.GetValue(5).ToString();//correct option.........

                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Errro 202");
            }
       
       
       
       
       
        } //method ends....................


        public static bool search(int[] x, int s)
        {
            bool c = false;
            for (int i = 0; i < x.Length; i++)
            {
                if (s == x[i])
                {

                    c = true;
                    break;
                }

            }

            return c;


        } //function of searching.....................




    }
}

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...