binding a database to a windows form
<p>I am very confused when it comes to binding the windows form to a database.</p>

<p>Basically i should be able to input the data via the windows form and it should get updated in the database.</p>

<p>Also i should be able to view the details when a particular id is input.</p>

<p>PLease do help</p>

<pre><code>namespace student_database {
    public partial class Form1 : Form
    {
        private SqlConnection connection;
        private SqlCommand command;

        private SqlDataAdapter adapter;
        private SqlCommandBuilder builder;
        private DataSet dataset;
        private bool updateReady;
        private bool insertReady;

        public Form1()
        {
            InitializeComponent();
            textBox1.Focus();
            textBox2.Focus();
            textBox3.Focus();
            textBox4.Focus();

            connection = new SqlConnection();
            command = connection.CreateCommand();
            connection.ConnectionString=@"Data Source=(local)" +
                                        "Initial Catalog=student details;Integrated Security=SSPI";

            command = connection.CreateCommand();
            adapter = new SqlDataAdapter(command);
            builder = new SqlCommandBuilder(adapter);
            dataset = new DataSet(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.CommandText = "SELECT * FROM student details WHERE StudentID=@StudentID";


            dataset.Tables.Clear();

            int result = adapter.Fill(dataset, "student details");

            if (result &gt; 0)
            {
                DataRow srow = dataset.Tables["student details"].Rows[0];
                textBox1.Text = srow["StudentID"].ToString();
                textBox2.Text = srow["Name"].ToString();
                textBox3.Text = srow["Age"].ToString();
                textBox4.Text = srow["CourseNo"].ToString();
            }
            else
            {
                Console.WriteLine("Student does not exist");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!insertReady)
            {
                button3.Text = "Enroll";
                //ClearFields();
                textBox1.Text = getnextid().ToString();

                textBox1.Enabled = false;
                textBox2.Enabled = true;
                textBox3.Enabled = true;
                textBox4.Enabled = true;

                button1.Enabled = false;
                button2.Enabled = false;
                button4.Enabled = false;

                insertReady = true;
            }
            else
            {
                button3.Text = "Add text";
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@Name", textBox2.Text);
                command.Parameters.AddWithValue("@Age", textBox3.Text);
                command.Parameters.AddWithValue("@Course", textBox4.Text);
                command.CommandText = "INSERT INTO Student_details name " + "(Name,Age,course) VALUES " +
                 "(@Name, @Age, @course)";

                try
                {
                    connection.Open();

                    int result = command.ExecuteNonQuery();

                    if (result &gt; 0)
                        Console.WriteLine("student successfully enrolled");
                    else
                        Console.WriteLine("Failed to enroll the student");

                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    connection.Close();
                }

                textBox1.Enabled = true;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
                textBox4.Enabled = false;
                button1.Enabled = true;
                button2.Enabled = true;
                button4.Enabled = true;
                insertReady = false;
            }
        }

            private void ClearFields()
            {
                textBox1.Text=String.Empty;
                textBox2.Text=String.Empty;
                textBox3.Text=String.Empty;
                textBox4.Text=String.Empty;
            }

            private int getnextid()
            {
                command.CommandText="SELECT IDENT_CURRENT('StudentID')+IDENT_INCR('StudentID')";

                try
                {
                    connection.Open();
                    int nextID=Convert.ToInt32(command.ExecuteScalar());
                    return nextID;
                }
                catch(SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
                return 0;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (!updateReady)
                {
                    button2.Text = "update";
                    textBox1.Enabled = false;
                    textBox2.Enabled = true;
                    textBox3.Enabled = true;
                    textBox4.Enabled = true;

                    button1.Enabled = false;
                    button3.Enabled = false;
                    button4.Enabled = false;

                    updateReady = true;
                }
                else
                {
                    button2.Text = "edit";
                    command.CommandText = "SELECT * FROM student name";
                    dataset.Tables.Clear();
                    adapter.Fill(dataset, "student name");

                    foreach (DataRow srow in dataset.Tables["student name"].Rows)
                    {
                        if (srow["StudentID"].ToString() == textBox1.Text)
                        {
                            srow["Name"] = textBox2.Text;
                            srow["Age"] = textBox3.Text;
                            srow["course"] = textBox4.Text;
                        }
                    }
                    try
                    {
                        int result = adapter.Update(dataset, "students");

                        if (result &gt; 0)
                            MessageBox.Show("Update successful.");
                        else
                            MessageBox.Show("Failed to update.");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }


                    textBox1.Enabled = true;
                    textBox2.Enabled = false;
                    textBox3.Enabled = false;
                    textBox4.Enabled = false;

                    button1.Enabled = true;
                    button3.Enabled = true;
                    button4.Enabled = true;
                    updateReady = false;

                }
            }

            private void button4_Click(object sender, EventArgs e)
            {
                command.CommandText = "SELECT * FROM Student details";
                dataset.Tables.Clear();
                adapter.Fill(dataset, "students");


                foreach (DataRow srow in dataset.Tables["students"].Rows)
                {
                    if (srow["StudentID"].ToString() == textBox1.Text)
                    {
                        srow.Delete();
                        break;
                    }
                }

                try
                {
                    int result = adapter.Update(dataset, "students");

                    if (result &gt; 0)
                        MessageBox.Show("Student successfully deleted.");
                    else
                        MessageBox.Show("Failed to delete student.");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                command.CommandText = "SELECT * FROM student details ";
                adapter.Fill(dataset, "Students");
            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {`enter code here`
                textBox1.Focus();
            }



        }
    }
</code></pre>
