Saturday, August 6, 2011

Binding Source

Binding Source:

Retrieving Information from database by using Binding Source:
  1. Establish the connection to the sql server by using sql connection class.
  2. SqlConnection cnn = new SqlConnection("Data Source = abc;
    Initial Catalog = master;Integrated Security= true");
  3. Execute the sql query to retrieve information by using sql data adapter.
  4. SqlDataAdapter da = new SqlDataAdapter("select * from student", cnn);
  5. Fill the values in Data Set by using the sql data adapter.
  6. da.Fill(ds, "student");
  7. Bind the dataset as data source to the binding source.
  8. bs.DataSource = ds.Tables["student"];
  9. Bind the values to the controls.
  10. textBox1.DataBindings.Add("Text", bs, "studnetid");
  11. Repeat the syntax for all the controls. Use the binding source methods to navigate between the values.
  12. bs.MoveFirst();bs.MovePrevious();bs.MoveNext();bs.MoveLast();
Complete code Listing Goes Here
public partial class Form1 : Form
    {
        SqlConnection cnn = new SqlConnection("Data Source = .\\SQLEXPRESS; 
  Initial Catalog = master;Integrated Security= true");
        BindingSource bs = new BindingSource(); DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cnn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from 
   student", cnn);
            da.Fill(ds, "student");
            bs.DataSource = ds.Tables["student"];
            textBox1.DataBindings.Add("Text", bs, "studnetid");
            textBox2.DataBindings.Add("Text", bs, "studnetname");
            textBox3.DataBindings.Add("Text", bs, "phoneno");
            textBox4.DataBindings.Add("Text", bs, "Address");
            textBox5.DataBindings.Add("Text", bs, "DOB");
            textBox6.DataBindings.Add("Text", bs, "Gender");
        }
        private void btnfirst_Click(object sender, EventArgs e)
        {
            bs.MoveFirst();
        }
        private void btnprevious_Click(object sender, EventArgs e)
        {
            bs.MovePrevious();
        }
        private void btnnext_Click(object sender, EventArgs e)
        {
            bs.MoveNext();
        }
        private void btnlast_Click(object sender, EventArgs e)
        {
            bs.MoveLast();
        }

    }

No comments:

Post a Comment