I have a ToolStripMenu and a table where I have a column named qcategory
char type. I want to create a query that selects only rows where qcategory
is equal with ToolStripMenuItem selected. So I tried in this way:
String categorie;
private void categoriaBToolStripMenuItem1_Click(object sender, EventArgs e)
{
Simulare sim = new Simulare();
sim.Show();
}
public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
{
categorie = simulareExamenToolStripMenuItem.Selected.ToString();
}
public string getCategorie()
{
return categorie;
}
What I did there was to create a string named categorie
. simulareExamenToolStripMenuItem
is the button from the menu. Here I assigned to categorie
the string value of item selected. In categoriaBToolStripMenu1
I instantiated form Simulare
where is the query. After that, I created a function that returns value of categorie
. Then, in Simulare
form, I instantiated Elev
form (where is menu).
Elev elev = new Elev();
After that, in constructor I assing to categorie
the value of categorie
from Elev
form.
String categorie = elev.getCategorie();
and do the query:
String dataA = "SELECT DISTINCT * FROM questions where `qcategory` = '" + categorie + "' order by rand() limit 1";
My problem is that it doesn't read the menu item value right and I can't find the problem. All in all, I have to pass from form Elev
, the string value of categorie
and use it in form Simulare
. Can anybody help me with that? Thanks!
UPDATE!
Now, this is crap. This is what I have in Elev
form:
public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
{
//categorie = simulareExamenToolStripMenuItem.Selected.ToString();
//SimulatorManager.Categorie = simulareExamenToolStripMenuItem.DropDownItems.ToString();
foreach (ToolStripMenuItem subItem in simulareExamenToolStripMenuItem.DropDownItems) //dropdown is the name of the **parent** of the dropdown. Without your full code I can't tell you what to put there
{
if(subItem.Checked)
{
SimulatorManager.Categorie = subItem.Text;
}
}
}
private void categoriaBToolStripMenuItem1_Click(object sender, EventArgs e)
{
Simulare sim = new Simulare();
sim.Show();
}
This is what I have in Simulare
form:
String categorie = SimulatorManager.Categorie;
and in contructor:
String dataA = "SELECT DISTINCT * FROM questions where `qcategory`='" + categorie + "' order by rand() limit 1";
But is not showing rows where exist CategoriaB
, it shows rows where value is Categoria C
. On Console.WriteLine(categorie)
it shows Categoria C
, not CategoriaB
as it should. (Categoria C
is a value like Categoria B
from qcategory
column, but at the other row.)
OMG! Whatever subItem I would chose, it selects Categoria C
..why???
UPDATE 2
This is what I have in Elev
form:
public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ToolStripMenuItem subItem in simulareExamenToolStripMenuItem.DropDownItems)
{
if(subItem.Checked)
{
SimulatorManager.Categorie = subItem.Text;
}
}
}
private void categoriaBToolStripMenuItem1_Click(object sender, EventArgs e)
{
Simulare sim = new Simulare();
sim.Show();
}
private void categoriaCToolStripMenuItem1_Click(object sender, EventArgs e)
{
Simulare sim = new Simulare();
sim.Show();
}
This is what I have in Simulare
form:
public Simulare() // maine constructor
{
String categorie = SimulatorManager.Categorie;
Console.WriteLine(categorie);
dataA = "SELECT DISTINCT * FROM questions where `qcategory`='" + categorie + "' order by rand() limit 1";
}
Whatever subItem I would choose, it selects rows which contains string value of last subItem from menu. (Categoria C
) If I click on Categoria B
, I receive questions from Categoria C
.