stu nicholls dot com | menu - Professional dropdown #3

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)
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
' ds1.AcceptChanges()
MsgBox("Update completed!")
Catch ex As Exception
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