Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Database Question and Answers Connect

C# Database Question and Answers Connect

Scheduled Pinned Locked Moved C#
questioncsharpdatabasetutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #1

    I created one table like this: CREATE TABLE Pitanja ( ID int NOT NULL IDENTITY (1,1) PRIMARY KEY, Pitanje nvarchar(1000) NOT NULL, Odgovor int NOT NULL, OpcijaA nvarchar(1000) NOT NULL, OpcijaB nvarchar(1000) NOT NULL, OpcijaC nvarchar(1000) NOT NULL, OpcijaD nvarchar(1000) NOT NULL ); This is one part of table Pitanja INSERT INTO Pitanja(Pitanje,Odgovor,OpcijaA,OpcijaB,OpcijaC,OpcijaD) VALUES('Blizu kog grada se nalaze Sremski Karlovci',3,'Rume','Sremske Mitrovice','Novog Sada','Šida'); Answers column contains number of correct choice,not word of correct choice.How to check answer when user select one of four choices from four buttons?

    Richard Andrew x64R 1 Reply Last reply
    0
    • P Pavlex4

      I created one table like this: CREATE TABLE Pitanja ( ID int NOT NULL IDENTITY (1,1) PRIMARY KEY, Pitanje nvarchar(1000) NOT NULL, Odgovor int NOT NULL, OpcijaA nvarchar(1000) NOT NULL, OpcijaB nvarchar(1000) NOT NULL, OpcijaC nvarchar(1000) NOT NULL, OpcijaD nvarchar(1000) NOT NULL ); This is one part of table Pitanja INSERT INTO Pitanja(Pitanje,Odgovor,OpcijaA,OpcijaB,OpcijaC,OpcijaD) VALUES('Blizu kog grada se nalaze Sremski Karlovci',3,'Rume','Sremske Mitrovice','Novog Sada','Šida'); Answers column contains number of correct choice,not word of correct choice.How to check answer when user select one of four choices from four buttons?

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      You could put the answers into an array like so:

      string[] Answers = new string[4];
      //
      //Code to load answers into array
      //
      string UserChoice = Answers[IndexOfChoice];

      Just set the variable IndexOfChoice to a number from 0 to 3 depending upon which button was clicked. IndexOfChoice is an int.

      The difficult we do right away... ...the impossible takes slightly longer.

      P 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        You could put the answers into an array like so:

        string[] Answers = new string[4];
        //
        //Code to load answers into array
        //
        string UserChoice = Answers[IndexOfChoice];

        Just set the variable IndexOfChoice to a number from 0 to 3 depending upon which button was clicked. IndexOfChoice is an int.

        The difficult we do right away... ...the impossible takes slightly longer.

        P Offline
        P Offline
        Pavlex4
        wrote on last edited by
        #3

        Here is complete code:

        int QuesionCount;
        int IndexOfChoice;
        DataTable dt = new DataTable();
        int[] Answers = new int[4];
        string cs= @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\pavle\documents\visual studio 2015\Projects\Ko zna zna\Ko zna zna\Koznazna.mdf;Integrated Security=True";
        private void Form1_Load(object sender, EventArgs e)
        {
        using (SqlConnection connection = new SqlConnection(cs))
        {
        string query = "SELECT * FROM Pitanja ORDER BY NEWID()";
        SqlCommand command = new SqlCommand(query, connection);
        try
        {
        connection.Open();
        dt.Load(command.ExecuteReader());
        connection.Close();
        }
        catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
        }
        }
        setQuestion();
        }

            private void provera()
            {
                int UserChoice = Answers\[IndexOfChoice\];
                if (answer\_id == UserChoice)
                {
                    MessageBox.Show("Odgovor je tacan!");
                }
                else
                {
                    MessageBox.Show("Odgovor nije tacan!");
                }
            }
        
            private void button1\_Click(object sender, EventArgs e)
            {
                Answers\[IndexOfChoice\] = 1;
                provera();  
            }
           
            private void button2\_Click(object sender, EventArgs e)
            {
                Answers\[IndexOfChoice\] = 2;
                provera();
            }
        
            private void button3\_Click(object sender, EventArgs e)
            {
                Answers\[IndexOfChoice\] = 3;
                provera();
            }
        
            private void button4\_Click(object sender, EventArgs e)
            {
                Answers\[IndexOfChoice\] = 4;
                provera();
            }
        
            private void button5\_Click(object sender, EventArgs e)
            {
                try
                {
                    this.QuesionCount++;
                    setQuestion();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            int answer\_id;
            private void setQuestion()
            { 
                try
                {
                    this.label2.Text = this.dt.Rows\[this.QuesionCount\]\["Pitanje"\].ToString();
                    this.button1.Text = this.dt.Rows\[th
        
        P 1 Reply Last reply
        0
        • P Pavlex4

          Here is complete code:

          int QuesionCount;
          int IndexOfChoice;
          DataTable dt = new DataTable();
          int[] Answers = new int[4];
          string cs= @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\pavle\documents\visual studio 2015\Projects\Ko zna zna\Ko zna zna\Koznazna.mdf;Integrated Security=True";
          private void Form1_Load(object sender, EventArgs e)
          {
          using (SqlConnection connection = new SqlConnection(cs))
          {
          string query = "SELECT * FROM Pitanja ORDER BY NEWID()";
          SqlCommand command = new SqlCommand(query, connection);
          try
          {
          connection.Open();
          dt.Load(command.ExecuteReader());
          connection.Close();
          }
          catch (Exception ex)
          {
          MessageBox.Show(ex.Message);
          }
          }
          setQuestion();
          }

              private void provera()
              {
                  int UserChoice = Answers\[IndexOfChoice\];
                  if (answer\_id == UserChoice)
                  {
                      MessageBox.Show("Odgovor je tacan!");
                  }
                  else
                  {
                      MessageBox.Show("Odgovor nije tacan!");
                  }
              }
          
              private void button1\_Click(object sender, EventArgs e)
              {
                  Answers\[IndexOfChoice\] = 1;
                  provera();  
              }
             
              private void button2\_Click(object sender, EventArgs e)
              {
                  Answers\[IndexOfChoice\] = 2;
                  provera();
              }
          
              private void button3\_Click(object sender, EventArgs e)
              {
                  Answers\[IndexOfChoice\] = 3;
                  provera();
              }
          
              private void button4\_Click(object sender, EventArgs e)
              {
                  Answers\[IndexOfChoice\] = 4;
                  provera();
              }
          
              private void button5\_Click(object sender, EventArgs e)
              {
                  try
                  {
                      this.QuesionCount++;
                      setQuestion();
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
              }
              int answer\_id;
              private void setQuestion()
              { 
                  try
                  {
                      this.label2.Text = this.dt.Rows\[this.QuesionCount\]\["Pitanje"\].ToString();
                      this.button1.Text = this.dt.Rows\[th
          
          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Who's writing this application, you or us? How do you think you should shuffle the cards?

          This space for rent

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            Who's writing this application, you or us? How do you think you should shuffle the cards?

            This space for rent

            P Offline
            P Offline
            Pavlex4
            wrote on last edited by
            #5

            I am creating new database.This is one part of it: {"a1":"Kina","a2":"Berlin","a3":"Živi","a4":"Cigla","a5":["Zid"],"b1":"Rešetke","b2":"Kriminal","b3":"Alkatraz","b4":"Kaucija","b5":["Zatvor"],"c1":"Artiljerija","c2":"Volt","c3":"Naboj","c4":"AA","c5":["Baterija","baterije"],"d1":"Bubreg","d2":"Nadležni","d3":"Tkivo","d4":"Transplantacija","d5":["Organ"],"rr":["Ćelija","ćelijski","celija","celijski"]} How to make all this as answer inside column Answers: ["Ćelija","ćelijski","celija","celijski"]

            P 1 Reply Last reply
            0
            • P Pavlex4

              I am creating new database.This is one part of it: {"a1":"Kina","a2":"Berlin","a3":"Živi","a4":"Cigla","a5":["Zid"],"b1":"Rešetke","b2":"Kriminal","b3":"Alkatraz","b4":"Kaucija","b5":["Zatvor"],"c1":"Artiljerija","c2":"Volt","c3":"Naboj","c4":"AA","c5":["Baterija","baterije"],"d1":"Bubreg","d2":"Nadležni","d3":"Tkivo","d4":"Transplantacija","d5":["Organ"],"rr":["Ćelija","ćelijski","celija","celijski"]} How to make all this as answer inside column Answers: ["Ćelija","ćelijski","celija","celijski"]

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              That is not a database. That is a fragment of JSON, and should be treated as such.

              This space for rent

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups