C# Create Menu Using MySQL

How To Make A Menu Using MenuStrip And  MySQL Database In C#

Create Menu Using MySQL Database In C#

In This C# Tutorial  We Will See How To Create A Menu Using MenuStrip And Add To This Menu Categories Using ToolStripMenuItem With Image And Text From MySQL Database Table And Add Also To This ToolStripMenuItem SubCategorys Elements From MySQL Database Using MySqlDataAdapter + DataTable + MemoryStream In Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace Csharp_Tutorials
{
    public partial class Create_Menu_From_MySQL : Form
    {
        public Create_Menu_From_MySQL()
        {
            InitializeComponent();
        }

        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
       
        private void Create_Menu_From_MySQL_Load(object sender, EventArgs e)
        {

            // add ToolStripMenuItem elements to menustrip using for loop
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM mypics", connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            for (int i = 0; i < table.Rows.Count; i++)
            {

                /*
                 * table => mypics
                 * table.Rows[i][3] = image column
                 * table.Rows[i][1] = name column
                 * table.Rows[i][1] = id column
                 */

                byte[] img = (byte[])table.Rows[i][3];
                MemoryStream ms = new MemoryStream(img);

                Image pic = Image.FromStream(ms);

                ToolStripMenuItem mit = new ToolStripMenuItem(table.Rows[i][1].ToString(), pic);

                addItems(mit, table.Rows[i][0].ToString());

                menuStrip1.Items.Add(mit);

            }

        }


        // create a function to add subElements to ToolStripMenuItem 
        public void addItems(ToolStripMenuItem MenuItem, string id)
        {

            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT idMyPics, name, pic FROM pics2 WHERE idMyPics = " + id, connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            for (int i = 0; i < table.Rows.Count; i++)
            {

                /*
                 * table => pics2
                 * table.Rows[i][2] = image column
                 * table.Rows[i][1] = name column
                 */

                byte[] img = (byte[])table.Rows[i][2];
                MemoryStream ms = new MemoryStream(img);

                Image pic = Image.FromStream(ms);

                MenuItem.DropDown.Items.Add(table.Rows[i][1].ToString(), pic);
            }

        }
    }
}

      

///////////////OUTPUT:

Make a Menu From Database In C#




Share this

Related Posts

Previous
Next Post »