VB.net(window): Connecting to SQL Server 2008 to fetch and insert row.

1. Create your database as

sql42.window form interface

sql1

3. VB Code

Imports System.Data.SqlClient
Public Class student_detail
    Private Sub BindData()
        Try
            Dim cnn As New SqlConnection
            Dim cmd As New SqlCommand
            cnn.ConnectionString = "data source=(local); initial catalog=student_record;user id='sa';password=''; integrated security=true;"
            cmd.CommandText = "select * from student"
            cmd.Connection = cnn
            cnn.Open()
            Dim ada As New SqlDataAdapter
            ada.SelectCommand = cmd
            Dim ds As New DataSet
            ada.Fill(ds, "student")
            DataGridView1.DataSource = ds.Tables("student")
            cnn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub student_detail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindData()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (String.IsNullOrEmpty(TextBox1.Text) Or String.IsNullOrEmpty(TextBox2.Text) Or
            String.IsNullOrEmpty(TextBox3.Text) Or String.IsNullOrEmpty(TextBox4.Text) Or
            String.IsNullOrEmpty(TextBox5.Text)) Then

            MessageBox.Show("Some fields are empty...!!")
           
        Else
            Try
                Dim cnn As New SqlConnection
                Dim cmd As New SqlCommand
                cnn.ConnectionString = "data source=.; initial catalog=student_record;user id='sa';password=''; integrated security=true;"
                cmd.CommandText = "insert into student(reg_no,sname,course,duration,address)  values(@reg_no,@sname,@course,@duration,@address)"
                cmd.Parameters.AddWithValue("@reg_no", (TextBox1.Text).ToUpper())
                cmd.Parameters.AddWithValue("@sname", (TextBox2.Text).ToUpper())
                cmd.Parameters.AddWithValue("@course", (TextBox3.Text).ToUpper())
                cmd.Parameters.AddWithValue("@duration", (TextBox4.Text).ToUpper())
                cmd.Parameters.AddWithValue("@address", (TextBox5.Text).ToUpper())
                cmd.Connection = cnn
                cnn.Open()
                Dim r As Integer = cmd.ExecuteNonQuery()
                If (r > 0) Then
                    MessageBox.Show("Data Saved Sucessfully")
                    BindData()
                Else
                    MessageBox.Show("Data Not Saved")
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
End Class

4. run the form and press the save button.

sql2

sql3