Showing posts with label c# combo. Show all posts
Showing posts with label c# combo. Show all posts

Dec 25, 2013

Load Year's To A ComboBox in C# That Are In Between Given Two Dates

This Code Unit Helps To Loads Years To A ComboBix in C#, That Are Between Two Given Dates.You Can Modify This Code Unit and You Can Get The Dates From Either User Input or Database Values.
 
In Order To Do That I'm Using A Function Called CmbYearLoad()
This Method Has Two String Variables Called sStartDate and sEndDate.
This Variables Stores The Values Of Dates. 


private void CmbYearLoad()
{
    string sStartDate = "06-22-2003";//This Value Can be Either From Database or User Input
    string sEndDate = "12-15-2013";
    DateTime dtStartDate = Convert.ToDateTime(sStartDate);
    DateTime dtEndDate = Convert.ToDateTime(sEndDate);
    int iStartYear = dtStartDate.Year;//Getting The Relevant Year Of The Date
    int iEndYear = dtStartDate.Year;
    CmbYear.Items.Clear();
    for (int iYearCount = iEndYear; iYearCount >= iStartYear; iYearCount--)
    {
        CmbYear.Items.Add(iYearCount);
    }
    CmbYear.SelectedIndex = 0;
}

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

Dec 1, 2013

C# Load Values To ComboBox From A Database

//you will need the my previous post . which i have explained about the DC Connection..
// I will call tht function and and connect the database;
// http://easycodestuff.blogspot.com/2013/11/c-sql-server-2008-database-connection.html

public void setSPCombo()  //ComboBox Load Functions
{
            DBConnection connection = new DBConnection();//make a object from the class DB Connection
            SqlConnection con = connection.ConnectDB();
            SqlCommand command = con.CreateCommand(); //creates a command with connection
 command.CommandText = "SELECT ColumnName From TableName ORDER  BY (ColumnName)";
//use order by if u want to order it in a-z format or 1-9 formats.

            SqlDataReader reader = command.ExecuteReader(); // u have to execute through a reader..

            comboname.Items.Clear(); //Fist clear the combobox because we dont want old the values
            comboname.Items.Add("Select"); //write a default vale and set it .
            comboname.SelectedIndex = 0;

            while (reader.Read())//read until the end of reader and add all the result rows to the combo
            {
               comboname.Items.Add(reader[0].ToString());
            }

         
            reader.Close(); // close the reader and con..we dont wont trouble later
            con.Close();
}

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