Showing posts with label C# Combo Box Index Changed. Show all posts
Showing posts with label C# Combo Box Index Changed. Show all posts

Jan 9, 2014

C# Load Data Into Data Grid On ComboBox Select Changed

private void COMBOBOXNAME_SelectedIndexChanged(object sender, EventArgs e)
{
      string Selecteditem = COMBOBOXNAME.Items[COMBOBOXNAME.SelectedIndex].ToString();
         
   string ConnectionString = @"Server=localhost;Database=dbname;Trusted_Connection=True";
 SqlConnection con = new SqlConnection(ConnectionString);
          try
            {
                con.Open();               
            }
          catch (SqlException ex)
            {
               
MessageBox.Show(ex.Message);
            }
         catch (Exception ex )
            {
                MessageBox.Show(ex.Message);
            }

            SqlCommand command = con.CreateCommand()

   command.CommandText = "SELECT * from Test";
         
   int iCount =0;

            SqlDataReader reader = command.ExecuteReader();
         
           DGName.Rows.Clear();
            while (reader.Read())
            {
                DGName.Rows.Add();
                DGName.Rows[iCount].Cells[0].Value = iCount + 1; // row number will be added
                DGName.Rows[iCount].Cells[1].Value = reader["DBColumnName"].ToString();
                DGName.[iCount].Cells[2].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[3].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[4].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[5].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[6].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[7].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[8].Value = reader["DBColumnName"].ToString();
             
                iCount++;
            }
            reader.Close();
           con.Close();

}

//DGName : Wnter Your Data Grid Name
//iCount : Counts The Rows
//COMBOBOXNAME : Enter Your ComboBoxName


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
        }
    }
}

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...