Showing posts with label c# load to combo from database. Show all posts
Showing posts with label c# load to combo from database. Show all posts

Jan 6, 2014

Auto Complete ComboBox in C# While Text Changed

This Program Used To Create A Combo Box In C# That Auto Complete With The Suggestions.
Create A Project and Add A Combo Box To It.




Just Make Sure You Have Change The Following Properties In The Combo Box Properties.




I Have Add Following Sample Data To My Combo Box.


using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComboBoxSuggest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Apple");
            comboBox1.Items.Add("Ball");
            comboBox1.Items.Add("Cat");
            comboBox1.Items.Add("Dog");
            comboBox1.Items.Add("Elephant");
            comboBox1.Items.Add("Fish");
            comboBox1.Items.Add("Goat");
            comboBox1.Items.Add("Animal");
            comboBox1.Items.Add("Amazon");
            comboBox1.Items.Add("Avatar");
        }
    }
}


Now Type And See,You Will Get Suggestions




If You Click The Drop down You Can See Everything...





If You Want To Add Dynamic Data To Combo Box You Can Do It Inside The Form Load Method.Following Example Load Data To Combo Box From SQL Server Database.


using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComboBoxSuggest
{
    public partial class MyForm : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void MyForm_Load(object sender, EventArgs e)
        {
            loadMyComboBox();
        }

        private void loadMyComboBox()
        { 
           string query = "SELECT EmpName,Age FROM Employee";
           DBConnect dbconnection = new DBConnect();
           DataTable datatable = new DataTable();
           
dbconnection.connect();        
           datatable = dbconnection.ReadData(query);

           MyComboBox.Items.Clear();//Clearing Existing Items
           foreach (DataRow row in dt.Rows)
           {
               
MyComboBox.ItemsAdd(row[0].ToString());
           }

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace ComboBoxSuggest
{
    class DBConnect
    {
        string strconnection= "Server=localhost;Database=dbname;Trusted_Connection=True;";
        SqlConnection sqlcon = new SqlConnection();
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        public void connect()
        {
            sqlcon = new SqlConnection(strconnection);
            sqlcon.Open();
        }
      
        public void disconnect()
        {
            if (sqlcon.State == ConnectionState.Open)
            {
                sqlcon.Close();
                sqlcon.Dispose();
            }
        }
      
        public DataTable ReadData(string query)
        {
            try
            {
                connect();
                sqlcmd = new SqlCommand(query, sqlcon);
                da = new SqlDataAdapter(sqlcmd);
                dt = new DataTable();
                da.Fill(dt);         
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                disconnect();
            }
            return dt;
        }
   
        public void QryCommand(string query)
        {
            try
            {
                connect();
                sqlcmd = new SqlCommand(query, sqlcon);
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                disconnect();
            }
        }
    }
}



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