.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Databinding in combobox

Posted By:satyapriyanayak       Posted Date: March 25, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

Databinding in combobox
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

namespace Dynamically_bind_data_to_combobox_from_database

{

    public partial class Form1 : Form

    {

        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

        OleDbCommand com;

        OleDbDataAdapter oledbda;

        DataSet ds;

        string str;

        DataTable dt;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

           

            OleDbConnection con = new OleDbConnection(ConnectionString);

            con.Open();

            str = "select * from student";

            com = new OleDbCommand(str, con);

            oledbda = new OleDbDataAdapter(com);

            ds = new DataSet();

            oledbda.Fill(ds, "student");

            dt = ds.Tables["student"];

            int i;

            for (i = 0; i <= dt.Rows.Count - 1; i++)

            {

                comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);

            }

            con.Close();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            OleDbConnection con = new OleDbConnection(ConnectionString);

            con.Open();

            str = "select * from student";

            com = new OleDbCommand(str, con);

            oledbda = new OleDbDataAdapter(com);

            ds = new DataSet();

            oledbda.Fill(ds, "student");

            comboBox2.DataSource = ds.Tables["student"];

            comboBox2.DisplayMember = "sname";

            con.Close();

 

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

           

            OleDbConnection con = new OleDbConnection(ConnectionString);

            con.Open();

            str = "select * from student";

            com = new OleDbCommand(str, con);

            OleDbDataReader reader = com.ExecuteReader();

            while (reader.Read())

            {

                comboBox3.Items.Add(reader["smarks"]);

            }

            reader.Close();

            con.Close();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            comboBox1.Text="Plz Click the button";

            comboBox2.Text="Plz Click the button";

            comboBox3.Text="Plz Click the button";

        }

    }

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend