0

I'm new to C# and I want to practise my skills on using parameters however I ran into a little trouble. I am designing a system where the system should view each question from the database whenever the student clicks on the 'view' button.

OBJECTIVE:

what I want to achieve is that when the user clicks on the button which is labelled 'next'. The system should 'view; the next question onto the datagrid. I thought maybe I should make a query which says something like "select question from ... where questionID = btnView. And maybe have a function where whenever the user clicks on the button it passes 1,2,3...10 as questionID (as there are 10 questions)

this is my C# Code:

try
{
string mydbConnection = "datasource=localhost;port=3306;Initial Catalog=project;username=***;password=***;";
MySqlConnection connDB = new MySqlConnection(mydbConnection);
MySqlCommand cmdDataBase = new MySqlCommand("SELECT questions.question, questions.answer FROM questions WHERE questionID ='" + questionID +  "' ;",connDB);
connDB.Open();
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource1 = new BindingSource();
sda.Update(dbdataset);
bSource1.DataSource = dbdataset;
dataGridView1.DataSource = bSource1;
sda.Update(dbdataset);
this.dataGridView1.Columns[3].Visible = false;
this.dataGridView1.Columns[0].Visible = false;
connDB.Close();
}

and this is my button function to get each 'questionID'

private void button1_Click(object sender, EventArgs e)
{
   for (int i = 1; i >= 10; i++)
  {
    int questionID = i;
  }
  viewQuestion(questionID);
}

questionID does not exist in the current context.

NOTE:

The stuff on the datagrid WORKS I just want it to view each question when the user clicks on the button.

EDIT:

  int questionID =  0;
  for (int i = 1; i >= 10; i++)
  {
    int questionID = i;
  }
  viewQuestion(questionID);

causes:

A local or parameter named 'questionID' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

1

1 Answer 1

3

when the user clicks on the button which is labelled 'next'. The system should 'view'.

So there is no need for a loop in a single click, You have to make the questionID as a global variable in the same class, and increment the value of questionID in each click. which means you can do something like this:

int questionID = 0;   // Global variable
private void button1_Click(object sender, EventArgs e)
{
   if(questionID <=10)
   {
      viewQuestion(questionID);
      questionID++;
   }
   else
   {
      // Display message that question over
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I would suggest not global but make it a member field.
@m.rogalski, of course it would be a field declared under the scope of the class.
But saying global means it wont be a member field but ( unique for each instance ) but one unified for all modules in that application scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.