Following is the Program to get connected to the Database, Retrieve data and Insert Record in the database using code.

Imports System.Data.SqlClient
Public Class Form1
Dim cnn As New SqlConnection
Dim da1 As SqlDataAdapter
Dim cb1 As SqlCommandBuilder
Dim ds1 As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnnstr As String = "Data Source=USER;Initial
Catalog=BCA;Integrated Security=True"
cnn.ConnectionString = cnnstr
da1 = New SqlDataAdapter("select * from BCAStdInfo", cnn)
cb1 = New SqlCommandBuilder(da1)
da1.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try
da1.Update(ds1.Tables(0))
' ds1.AcceptChanges()
MsgBox("Update completed!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

The form is designed as follows:


Run the Application you get the following screen shot


Insert One More record and click on to Update command the record is inserted which is shown in the next screen shot.


Note: If you try to delete a row from the database and try to click on to Update button you will get the message as shown in the following screen shot.



Create DataBase<< Previous

Next >>More Examples


comments powered by Disqus




Footer1