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
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.