Set new cell value in DataSet : DataSet Update « Database ADO.net « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Database ADO.net » DataSet UpdateScreenshots 
Set new cell value in DataSet
Set new cell value in DataSet

Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class MainClass

    Shared Sub Main()
        Dim form1 As Form = New Form1
        Application.Run(form1)
    End Sub
End Class









Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is NothingThen
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
        Me.button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'button1
        '
        Me.button1.Font = New System.Drawing.Font("Microsoft Sans Serif"9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.button1.Location = New System.Drawing.Point(108)
        Me.button1.Name = "button1"
        Me.button1.Size = New System.Drawing.Size(24064)
        Me.button1.TabIndex = 1
        Me.button1.Text = "Update a record"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(26081)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.Text = "Row Updating"
        Me.ResumeLayout(False)

    End Sub

#End Region

        Dim dbConn As New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
    Dim WithEvents da As New SqlDataAdapter("SELECT ID, FirstName, LastName FROM Employee", dbConn)

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles button1.Click

        Dim cb As New SqlCommandBuilder(da)
        Dim ds As New DataSet("Employee")

        da.Fill(ds)
        ds.Tables(0).Rows(0)("ID""1"
        da.Update(ds.GetChanges())
    End Sub


    Private Sub da_RowUpdating(ByVal sender As Object, _
                               ByVal e As SqlRowUpdatingEventArgs_
                               Handles da.RowUpdating

        ' Only perform processing if this is an UPDATE statement
        If (e.StatementType = StatementType.UpdateThen

            ' Check that the original record is not changed
            Dim strSQL As String = "SELECT ID, FirstName, LastName " & _
                                   "FROM Employee WHERE "
            strSQL &= "ID='" & e.Row("ID", DataRowVersion.Original)
            strSQL &= "' AND "
            strSQL &= "FirstName='" & e.Row("FirstName", DataRowVersion.Original)
            strSQL &= "' AND "
            strSQL &= "LastName='" & e.Row("LastName", DataRowVersion.Original"'"

            ' Open the connection
            dbConn.Open()

            ' Create a command to retrieve the number of records affected
            Dim cmm As New SqlCommand(strSQL, dbConn)

            ' If the number of records retrieved is zero, the record has changed
            If (cmm.ExecuteNonQuery() 0Then
                ' Display the confirm message
                If (MessageBox.Show("The record you are attempting to modify has " & _
                                    "already changed by another user. Do you " & _
                                    "want to overwrite it?""Update", _
                                    MessageBoxButtons.YesNo= DialogResult.NoThen

                    ' Skip the update for the current row
                    e.Status = UpdateStatus.SkipCurrentRow
                End If
            End If

            ' Close the connection
            dbConn.Close()

        End If
    End Sub
End Class

           
       
Related examples in the same category
1.Modify Data table: add a row and update a cellModify Data table: add a row and update a cell
2.Use DataSet to query and update RowUse DataSet to query and update Row
3.Update a row in DataSet update event
w__w__w___.j_a_v___a2s.c___om___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.