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();
}

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