C# Add Item To Menu From MySQL

How To Add A New Item To MenuStrip From MySQL Database In C#

Add Item To MenuStrip From MySQL In C#

In This C# Tutorial  We Will See How To Create A New ToolStripMenuItem With Image And Text From MySQL Database Table And Add This ToolStripMenuItem To A MenuStrip Element 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 Add_Item_To_MenuStrip_From_MySQL : Form
    {
        public Add_Item_To_MenuStrip_From_MySQL()
        {
            InitializeComponent();
        }

// create a connection with the database
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
        
        private void Add_Item_To_MenuStrip_From_MySQL_Load(object sender, EventArgs e)
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM mypics", connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            for (int i = 0; i < table.Rows.Count; i++)
            {
// get the image from the database
                byte[] img = (byte[])table.Rows[i][3];
                MemoryStream ms = new MemoryStream(img);

                Image pic = Image.FromStream(ms);

// create a new toolstripmenuitem with text and image
                ToolStripMenuItem mitem = new ToolStripMenuItem(table.Rows[i][1].ToString(), pic);

// add our toolstripmenuitem to the menustrip
                menuStrip1.Items.Add(mitem);
            }
        }
    }
}

      

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

Add Item To MenuStrip From Database Using C#





Share this

Related Posts

Previous
Next Post »