Monday, April 30, 2012

Coding for load the data in ComboBox from the database in C#

private SqlConnection GetConnection()
        {
            SqlConnection myconnection = new SqlConnection();
            myconnection.ConnectionString         = "server=MD-TRNGLP59\\SQLEXPRESS; initial catalog=Sample; integrated security=true";
            return cn;
        }


private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection myconnection = GetConnection();
            SqlCommand mycommand = myconnection.CreateCommand();
            mycommand.CommandText = "select accountid from account";
            
            try
            {
                myconnection.Open();
                SqlDataReader dr = mycommand.ExecuteReader();
                while (dr.Read())
                {
                    if (dr[0].ToString() != null)
                        comboBox1.Items.Add(dr[0]);
                   
                }


            }

            catch (Exception err)
            {
                MessageBox.Show("Error", err.Message);
            }

            finally
            {
                myconnection.Close();
            }
}

--
By
Rajkumar Rajarathnam Subramanian
<http://www.rsrajkumar.blogspot.com>*








No comments:

Post a Comment

Followers