Last active
May 1, 2017 01:12
-
-
Save NPS-ARCN-CAKN/761749ebfad1045ea861a9cb94786c23 to your computer and use it in GitHub Desktop.
VB .Net: Basic example of inserting new records using a DataGridView, Dataset and DataAdapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'See https://msdn.microsoft.com/en-us/library/33y2221y(v=vs.110).aspx for more information | |
Imports System.Data.SqlClient | |
Public Class Form1 | |
'create a dataset | |
Dim AKRODataset As New DataSet("AKRODataset") | |
'set up the database connectionstring | |
Dim tblVitalSignWorkLogSqlConnection As New SqlConnection(My.Settings.ConnectionString) | |
'build a dataadapter for the work log table | |
Dim tblVitalSignWorkLogSelectQuery As String = "SELECT [VSDMLogID],[LogDate],[Username],[LogEntry],[VSID] FROM [AKRO].[dbo].[tblVitalSignWorkLog] ORDER BY LogDate DESC" | |
Dim tblVitalSignWorkLogSelectSqlCommand As New SqlCommand(tblVitalSignWorkLogSelectQuery, tblVitalSignWorkLogSqlConnection) | |
Dim tblVitalSignWorkLogDataAdapter As New SqlDataAdapter(tblVitalSignWorkLogSelectSqlCommand) | |
'create a binding source for the work log datatable | |
Dim tblVitalSignWorkLogBindingSource As New BindingSource() | |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load | |
Me.WindowState = FormWindowState.Maximized | |
'fill the dataset's work log table using the dataadapter | |
tblVitalSignWorkLogDataAdapter.Fill(AKRODataset, "tblVitalSignWorkLog") | |
'set up the work log data adapter | |
Dim tblVitalSignWorkLogInsertQuery As String = "INSERT INTO tblVitalSignWorkLog(VSID,LogEntry) VALUES(@VSID,@LogEntry);" | |
Dim tblVitalSignWorkLogInsertSqlCommand As New SqlCommand(tblVitalSignWorkLogInsertQuery, tblVitalSignWorkLogSqlConnection) | |
tblVitalSignWorkLogDataAdapter.InsertCommand = tblVitalSignWorkLogInsertSqlCommand | |
Dim VSIDParameter As New SqlParameter("@VSID", SqlDbType.Int) | |
VSIDParameter.SourceColumn = "VSID" | |
VSIDParameter.SourceVersion = DataRowVersion.Default | |
tblVitalSignWorkLogDataAdapter.InsertCommand.Parameters.Add(VSIDParameter) | |
Dim LogEntrParameter As New SqlParameter("@LogEntry", SqlDbType.VarChar) | |
LogEntrParameter.SourceColumn = "LogEntry" | |
LogEntrParameter.SourceVersion = DataRowVersion.Default | |
tblVitalSignWorkLogDataAdapter.InsertCommand.Parameters.Add(LogEntrParameter) | |
'set up the work log binding source | |
tblVitalSignWorkLogBindingSource.DataSource = AKRODataset | |
tblVitalSignWorkLogBindingSource.DataMember = "tblVitalSignWorkLog" | |
Me.tblVitalSignWorkLogBindingNavigator.BindingSource = tblVitalSignWorkLogBindingSource | |
Me.VitalSignsDataGridView.DataSource = tblVitalSignWorkLogBindingSource | |
End Sub | |
Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click | |
tblVitalSignWorkLogBindingSource.EndEdit() | |
'if the dataset has changes ask to save them | |
If AKRODataset.HasChanges Then | |
If MsgBox("Save changes", MsgBoxStyle.YesNo, "Save?") = vbYes Then | |
Try | |
tblVitalSignWorkLogDataAdapter.Update(AKRODataset.Tables(0).GetChanges(DataRowState.Added)) | |
AKRODataset.AcceptChanges() | |
Catch ex As Exception | |
MsgBox("Save failed" & ex.Message) | |
AKRODataset.RejectChanges() | |
End Try | |
End If | |
End If | |
End Sub | |
Private Sub VitalSignsDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles VitalSignsDataGridView.SelectionChanged | |
AKRODataset.Tables(0).Columns("VSID").DefaultValue = tblVitalSignWorkLogBindingSource.Current("VSID") | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment