Jump to content

Converting PHP Code to C# in Visual Studio Windows Form

Go to solution Solved by flexluthor,

Nevermind, solved it. The problem was it was using read multiple times (apparently you could only read one by default) so the solution was just adding a code to allow multiple reads on the connection string of the database.

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:

private void Form1_Load(object sender, EventArgs e){sc.Open();//For Main CategorySqlCommand 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 CategorySqlCommand 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 InventorySqlCommand 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:

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.
Link to comment
Share on other sites

Link to post
Share on other sites

Nevermind, solved it. The problem was it was using read multiple times (apparently you could only read one by default) so the solution was just adding a code to allow multiple reads on the connection string of the database.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×