Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a somewhat okay knowledge in Php but I recently started Visual Studio and I was wondering how would I go about converting my code to Visual Studio. I wanted to create a combo box that will print out the main category then the sub category under it then the items under it and so on until all main categories are finished. I had no idea how to write them on C# so I first created the code in PHP and tried to dissect them.

So far my current code in Visual Studio is:
C#
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
SqlCommand get_subcategory_name = new SqlCommand("SELECT * FROM subcategory WHERE maincategory_id='1'", sc);
SqlDataReader dr3 = get_subcategory_name.ExecuteReader();
while (dr3.Read())
{
comboBox1.Items.Add("--" + dr3["subcategory_name"].ToString());

//For Inventory
SqlCommand get_inventory_name = new SqlCommand("SELECT * FROM inventory WHERE subcategory_id='1'", sc);
SqlDataReader dr4 = get_inventory_name.ExecuteReader();
while (dr4.Read())
{
comboBox1.Items.Add("----" + dr4["item_name"].ToString());
}
}
}
sc.Close();
}


My PHP code looks like this:
PHP
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>";


The problems I'm having:
1. It's only showing the first main category name and nothing else.
2. I'm currently figuring out how to put dr2["maincategory_id"] (to replace the 1) inside the sql statement under WHERE but I'm having trouble figuring out what's the syntax on how it wouldn't be interpreted as a string.
3. I don't know how to disable(or to make read-only) certain parts of the combo box.
Posted
Comments
Amit Jadli 16-Oct-15 1:44am    
You could simply put that value like '"+ dr2["maincategory_id"] +"'
Sinisa Hajnal 16-Oct-15 3:25am    
Do you really need all categories and subcategories in the same dropdown? Wouldn't it be better to load each as needed? In several dropdowns containing hierarchy of categories?

1 solution

This is not how you bind comboboxes in .NET.

You should do something like this:
C#
SqlCommand getMainCategoryName = new SqlCommand("SELECT * FROM maincategory", sc);
DataSet ds = new DataSet;
SqlDataAdapter da = new sqlDataAdapter;
da.SelectCommand = getMainCategoryName ;
da.Fill(ds);

// display member is the column that will show in the combobox
cboMainCategoriesDisplayMember = "maincategory_name";
// value member is the column that will be the value of the combobox
cboMainCategories.ValueMember = "maincategory_id"
cboMainCategories.DataSource = ds; // you may put ds.Tables[0];



Note that control names are now non-default meaningful names. I've named variables as I prefer, you can keep yours of course :). I would suggest you change your id fields to integer, but again, your choice. It helps with sorting by ID and joins are faster.

You can write dr2["maincategory_id"].ToString(); to get the string out of the column.
 
Share this answer
 
v2

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