Click here to Skip to main content
16,022,542 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Am developing an exam software which fetch out the questions from a database, In the database i have these columns Exam_ID, Question, Option1, Option2, Option3, Option4, Answer, the question is displayed in a textbox on the WinForm, while from option1 to Option4 are displayed on a radio button, am done with the next and previous buttons, but what i need now is when the user clicks next, it would match clicked Option with the right answer in the db.


C#
namespace WpfAppEEXam
{
    /// <summary>
    /// Interaction logic for Questions.xaml
    /// </summary>
    public partial class Questions : Window
    {
        int rowIndex = 0;
        SqlDataReader rdr = null;
        DataTable dtable = new DataTable();
        SqlConnection con = null;
        SqlDataAdapter adp;
        DataSet ds = new DataSet();
        SqlCommand cmd = null;
        DataTable dt = new DataTable();
        //string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MrKuzz\Desktop\My Work\AdminPanel\AdminPanel\EExamDB.mdf;Integrated Security=True;User Instance=True";
        /// <summary>
        /// 
        /// </summary>
        private int time = 30;
        private DispatcherTimer Timer;

        public Questions()
        {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = new TimeSpan(0, 0, 1);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dt = GetData();
                if(dt.Rows.Count > 0)
                {
                    textBox1.Text = dt.Rows[0]["question"].ToString();

                    radioButton1.Content = dt.Rows[0]["option1"];
                    radioButton2.Content = dt.Rows[0]["option2"];
                    radioButton3.Content = dt.Rows[0]["option3"];
                    radioButton4.Content = dt.Rows[0]["option4"];
                    radioButton5.Content = dt.Rows[0]["option5"];
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        void Timer_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {
                if(time <=600)
                {
                    
                    if (time % 2 == 0)
                    {
                        textBlockTime.Foreground = Brushes.Red;
                        textBlockwarning.Text = "Running out of Time";
                        textBlockwarning.Foreground = Brushes.OrangeRed;
                    }
                    else
                    {
                        textBlockTime.Foreground = Brushes.Black;
                    }
                    time--;
                    textBlockTime.Text = string.Format("{0:00h} " + ":" + " {1:00m} " + ":" + " {2:00s}", time / 3600, (time % 3600) / 60, time % 60);
                }
                else
                {
                    time--;
                    textBlockTime.Text = string.Format("{0:00h} " + ":" + " {1:00m} " + ":" + " {2:00s}", time / 3600, (time % 3600) / 60, time % 60);

                }
            }
            else
            {
                Timer.Stop();
                MainWindow mw = new MainWindow();
                mw.Show();
                this.Hide();

            }
        }

        
        
        //Buton that fetch next record
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (rowIndex < dt.Rows.Count - 1)
            {
                rowIndex++;
                textBox1.Text = dt.Rows[rowIndex]["question"].ToString();

                radioButton1.Content = dt.Rows[rowIndex]["option1"];
                radioButton2.Content = dt.Rows[rowIndex]["option2"];
                radioButton3.Content = dt.Rows[rowIndex]["option3"];
                radioButton4.Content = dt.Rows[rowIndex]["option4"];
                radioButton5.Content = dt.Rows[rowIndex]["option5"];
            }
            else
            {
                
            }
        }
        //Buton that fetch previous record
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (rowIndex == dt.Rows.Count - 1 || rowIndex != 0)
            {
                rowIndex--;
                textBox1.Text = dt.Rows[rowIndex]["question"].ToString();

                radioButton1.Content = dt.Rows[rowIndex]["option1"];
                radioButton2.Content = dt.Rows[rowIndex]["option2"];
                radioButton3.Content = dt.Rows[rowIndex]["option3"];
                radioButton4.Content = dt.Rows[rowIndex]["option4"];
                radioButton5.Content = dt.Rows[rowIndex]["option5"];
            }
            else
            {
                
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                e.Cancel = true;
                //base.OnClosing(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MrKuzz\Desktop\My Work\ExamProject\Project DataBase File For Exam\EExamDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            try
            {
                connection.Open();
                SqlCommand sqlCmd = new SqlCommand("Select * From Questions WHERE classid='"+UserClass.CurrentClass+"' ", connection);
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

                sqlDa.Fill(dt);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);

            }
            finally
            {
                connection.Close();
            }
            return dt;
        }
         
    }
}
Posted
Updated 1-Nov-14 4:24am
v2
Comments
OriginalGriff 1-Nov-14 9:50am    
And?
What have you tried?
Where are you stuck?
What help do you need?
Member 11197984 1-Nov-14 10:20am    
wld post my code structure
DamithSL 1-Nov-14 10:02am    
Ok now you want us to write that part of your application?
Member 11197984 1-Nov-14 10:19am    
if possible

1 solution

DamithSL at 19 mins ago
Ok now you want us to write that part of your application?

Member 11197984 at 2 mins ago
if possible


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
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