Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im trying to calculate marks of every ques but as and when the increment happens it loads the first question again and again and if i give the variable as 2 it will never enter the place where marks are calculated . if ii repeats with question 1 then we need to ans for next ques with previous question as it is loading again question 1

What I have tried:

C#
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.IO;
using System.Data.SqlClient;



namespace trial__
{
    public partial class Form1 : Form
    {
        public int incre = 1;
        public int b;
        public string y;
        public string z;
        public int marks;

        public Form1()
        {
            InitializeComponent();
            radioButton1.TabStop = false;
            radioButton2.TabStop = false;
            radioButton3.TabStop = false;
            radioButton4.TabStop = false;
           
            marks = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //
            String Source = "xxxx";            
            SqlConnection con_2 = new SqlConnection(Source);
            {
                con_2.Open();
                MessageBox.Show("connected to db");
                string a = incre.ToString();
                string sqlSelectQuery = @"SELECT *FROM [Table]";
                SqlCommand cmd = new SqlCommand(sqlSelectQuery, con_2);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    label1.Text = (dr["ques"].ToString());
                    textBox1.Text = (dr["sl"].ToString());
                    radioButton1.Text = (dr["op A"].ToString());
                    radioButton2.Text = (dr["op B"].ToString());
                    radioButton3.Text = (dr["op C"].ToString());
                    radioButton4.Text = (dr["op D"].ToString());
                    
                  
                }
            }
            con_2.Close();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string a = incre.ToString();
            String Source ="xxxx";
            SqlConnection con_2 = new SqlConnection(Source);
            {
                con_2.Open();
              
                string sqlSelectQuery = @"SELECT *FROM [Table] WHERE sL=" + a;
                SqlCommand cmd = new SqlCommand(sqlSelectQuery, con_2);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    label1.Text = (dr["ques"].ToString());
                    textBox1.Text = (dr["sl"].ToString());
                    radioButton1.Text = (dr["op A"].ToString());
                    radioButton2.Text = (dr["op B"].ToString());
                    radioButton3.Text = (dr["op C"].ToString());
                    radioButton4.Text = (dr["op D"].ToString());


                    if (radioButton1.Checked == true)
                    {
                        y = dr["ans"].ToString();
                        z = dr["marks"].ToString();
                        int bi = Convert.ToInt32(z);
                        
                        //ans.Text = "meg";
                        if (radioButton1.Text == y)
                        {

                            marks = marks +bi;
                        }


                    }
                    if (radioButton2.Checked == true)
                    {
                        string g = (dr["ans"].ToString());
                        string h = dr["marks"].ToString();
                        int bd = Convert.ToInt32(h);
                        

                        if (radioButton2.Text == g)

                        {
                            marks = marks + bd;
                        }


                    }

                    if (radioButton3.Checked == true)
                    {
                        string g = (dr["ans"].ToString());
                        string h = dr["marks"].ToString();
                        int bk = Convert.ToInt32(h);


                        if (radioButton3.Text == g)

                        {
                            marks = marks + bk;
                        }


                    }

                    if (radioButton4.Checked == true)
                    {
                        string g = (dr["ans"].ToString());
                        string h = dr["marks"].ToString();
                        int bl = Convert.ToInt32(h);


                        if (radioButton4.Text == g)

                        {
                            marks = marks + bl;
                        }


                    }


                }

            }
            
            con_2.Close();
            incre++;
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
Posted
Updated 11-Jun-18 19:32pm
v2
Comments
Richard Deeming 12-Jun-18 11:14am    
string sqlSelectQuery = @"SELECT *FROM [Table] WHERE sL=" + a;


Don't do it like that! Whilst in this specific scenario nothing bad is going to happen, writing code like that can and will lead to SQL Injection[^] vulnerabilities.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

using (SqlCommand cmd = new SqlCommand("SELECT * FROM [Table] WHERE sL = @sL", con_2))
{
    cmd.Parameters.AddWithValue("@sL", incre);
    
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        ...
    }
}
Richard Deeming 12-Jun-18 11:17am    
Also, remember that fields in your class will not be persisted between postbacks. Every time the user clicks the button, a new instance of your class is created, with the fields initialized to their default values.

If you need to store a value between requests, consider using view-state instead:
public int incre
{
    get { return (int?)ViewState["incre"] ?? 1; }
    set { ViewState["incre"] = value; }
}

1 solution

Without your database we can't run your code, and that's exactly what is needed here - you can't workout what is going wrong without running the code and looking at variables as it goes. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But ... your "check the marks" code is rather odd. You appear to read the current question from the db, and set the label texts to match it, then immediately mark it based on the users last set of responses.
You would probably be better off splitting this into two separate methods: one to mark the current question, then a second to load a specific question. Call the second from your Load event handler to set everything up, then in your button Click event, call eth first to mark it, then the second to load a new question. It just makes it clearer what is going on, and easier to maintain and even develop!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900