I have a combo box that's supposed to output the main category (has 4 rows) then also output the sub category under it (ex. 3 sub categories under the first main category, 5 sub categories under the second main category and so on).
My First Code displays the main category correctly (the combobox has 4 results)
private void Form1_Load(object sender, EventArgs e)
{
sc.Open();
//For Main Category
SqlCommand get_maincategory_name = new SqlCommand("SELECT * FROM maincategory", sc);
SqlDataReader dr2 = get_maincategory_name.ExecuteReader();
while (dr2.Read())
{
comboBox1.Items.Add(dr2["maincategory_name"].ToString());
}
sc.Close();
}
But I tried inserting another loop for the sub category under the while loop of the main category but all I'm getting on the combo box is the first row of the main category (the combobox only has 1 result which is the first row from the main category table)
The code I used was:
private void Form1_Load(object sender, EventArgs e)
{
sc.Open();
//For Main Category
SqlCommand get_maincategory_name = new SqlCommand("SELECT * FROM maincategory", sc);
SqlDataReader dr2 = get_maincategory_name.ExecuteReader();
while (dr2.Read())
{
comboBox1.Items.Add(dr2["maincategory_name"].ToString());
//For Sub Category
int got_category_id = Convert.ToInt32(dr2["maincategory_id"]);
SqlCommand get_subcategory_name = new SqlCommand("SELECT * FROM subcategory WHERE maincategory_id='got_category_id'", sc);
SqlDataReader dr3 = get_subcategory_name.ExecuteReader();
while (dr3.Read())
{
comboBox1.Items.Add(dr3["subcategory_name"].ToString());
}
}
sc.Close();
}
The only programming experience I have is on php so I tried creating my intended code on PHP and it worked perfectly but I'm having troubles applying it to C# in Visual Studio, here's my code if you guys are wondering what I'm trying to achieve:
echo "<select>";
// FOR MAIN CATEGORY
$maincategory_query = mysqli_query($con,"SELECT * FROM maincategory") or mysqli_error();
while($got = mysqli_fetch_assoc($maincategory_query))
{
$maincategory_id = $got['maincategory_id'];
$maincategory_name = $got['maincategory_name'];
echo "<option disabled>$maincategory_name</option>";
// FOR SUB CATEGORY
$subcategory_query = mysqli_query($con,"SELECT * FROM subcategory WHERE maincategory_id='$maincategory_id'") or mysqli_error();
while($got = mysqli_fetch_assoc($subcategory_query))
{
$subcategory_id = $got['subcategory_id'];
$subcategory_name = $got['subcategory_name'];
echo "<option disabled>--$subcategory_name</option>";
// FOR ITEM
$item_query = mysqli_query($con,"SELECT * FROM item WHERE subcategory_id='$subcategory_id'") or mysqli_error();
while($got = mysqli_fetch_assoc($item_query))
{
$item_id = $got['item_id'];
$item_name = $got['item_name'];
echo "<option>----$item_name</option>";
}
}
}
echo "</select>";
My Concerns:
I don't actually know what's happening on why my second code is only doing the loop once and what's worse is it doesn't even display the sub category (the
maincategory_id='got_category_id'
beside the WHERE in my sql statement was only a placeholder and I tried replacing it bymaincategory_id='1'
just to check in hopes of it trying to at least check if it's displaying the sub category to no avail. I actually don't know the proper syntax to insert an integer inside a sql statement in Visual Studio but nevertheless, setting it to a value of '1' still doesn't display anything).What is the proper syntax on inserting an integer inside a sql statement in Visual Studio? I researched and tried putting the & at the beginning and end of the variable but it isn't working.