Dec 24, 2013

Load Values Directly From Database To A Combo Box In C# With Another Combo Box Index Change Event

In My Example I'm Trying To Load District And Cities To Two Combo Box
First Make Sure Your Connection Is As Below
(if you have any problem in SQL Connection refer to following post
http://easycodestuff.blogspot.com/2014/01/secure-best-sql-server-connection-for-c.html)

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

namespace MyProject
{
    class DBConnection
    {
        public MySqlConnection ConnectDB()
        {
            MySqlConnection con = new MySqlConnection("Server = localhost ; Uid = root ; Pwd = ; Database = example;"); //With PhpMyAdmin Database
            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return con;
        }
    }
}

following codes help to loads the Combo Box

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

namespace MyProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cmbDistrictLoad();//To Load Items To The District ComboBox
        }
        private void cmbDistrictLoad()
        {
            DBConnection connection = new DBConnection();
            MySqlConnection con = connection.ConnectDB();
            MySqlCommand command = con.CreateCommand();
            command.CommandText = "SELECT DistrictName From District Order By (DistrictName)";
            MySqlDataReader reader = command.ExecuteReader();
            cmbDistrict.Items.Clear();
            if (reader.HasRows == true)
            {
                while (reader.Read())
                {
                    cmbDistrict.Items.Add(reader[0].ToString());
                }
            }
            cmbDistrict.SelectedIndex = 0;//Index Changed
        }
        private void cmbCityLoad(string SelectedDistrict)
        {
            DBConnection connection = new DBConnection();
            MySqlConnection con = connection.ConnectDB();
            MySqlCommand command = con.CreateCommand();
            command.CommandText = "SELECT CityName From City WHERE DistrictId = (SELECT DistrictId From District WHERE DistrictName = '"+ SelectedDistrict +"') Order By (CityName)";
            MySqlDataReader reader = command.ExecuteReader();
            cmbCity.Items.Clear();
            if (reader.HasRows == true)
            {
                while (reader.Read())
                {
                    cmbCity.Items.Add(reader[0].ToString());
                }
            }
            cmbCity.SelectedIndex = 0;//Index Changed
        }
        private void cmbDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SelectedDistrict = cmbDistrict.Items[cmbDistrict.SelectedIndex].ToString();
            cmbCityLoad(SelectedDistrict);//Calling cmbCityLoad() With selected District
        }
        private void cmbCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            //You Can Write Your Code As You Wish After Select A District
        }
    }
}

No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...