C
Charles
Because I am still new at this I am not sure what I am
missing. I am having a problem with getting the Textbox
value to update the second time. For example, I change
the value of the textbox two times for whatever reason.
I get an error message of "Concurrency violation: the
UpdateCommand affected 0 records." on the second change.
Why am I getting that error message? Is this a good
example of a DAL? If it is not where could I find one?
Any kind of help will be great.
I have a class (DataAccess) that I am calling from a
Windows Form. The class is connecting to the NorthWind
database using the Suppliers table. The Windows Form has
a Textbox (ContactName) and a Button (Save).
My code below is based off from Microsoft's "N-Tier Data
Form and Data Layer" example.
Thanks in Advance
Charles
--DataAccess Class--
Option Strict On
Imports System.Data.SqlClient
Public Class DataAccess
Protected strConn As String
= "server=localhost;database=northwind;integrated
security=sspi"
Protected DidPreviouslyConnect As Boolean = False
Private daSuppliers As SqlDataAdapter
Public Function CreateDataSet() As DataSet
Dim dsSupplier As DataSet
Dim IsConnecting As Boolean = True
While IsConnecting
Try
Dim SQLConn As New SqlConnection(strConn)
Dim strSQL As String = "SELECT * FROM
Suppliers"
Dim Cmd As New SqlCommand(strSQL, SQLConn)
daSuppliers = New SqlDataAdapter(Cmd)
Dim CmdBuild As New SqlCommandBuilder
(daSuppliers)
dsSupplier = New DataSet
daSuppliers.Fill(dsSupplier, "Supplier")
IsConnecting = False
DidPreviouslyConnect = True
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return dsSupplier
End While
End Function
Public Sub UpdateDataSet(ByVal inDS As DataSet)
If inDS Is Nothing Then
Exit Sub
End If
Try
If (Me.daSuppliers Is Nothing) Then
CreateDataSet()
End If
inDS.EnforceConstraints = False
daSuppliers.Update(inDS, "Supplier")
Catch exc As Exception
Debug.WriteLine(exc.Message)
End Try
End Sub
End Class
--Form1 Class--
Option Strict On
Imports System.Data.SqlClient
Imports DataAccessLayertrl1
Public Class Form1
Inherits System.Windows.Forms.Form
Protected DidPreviouslyConnect As Boolean = False
Private dsSupplier As DataSet
Private dtSupplier As DataTable
Private dvSupplier As DataView
Dim m_DAL As DataAccess
'#Region " Windows Form Designer generated code " Here
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
GetDataSet()
DataBindControls()
End Sub
Sub DataBindControls()
txtContactName.DataBindings.Add("Text",
dvSupplier, "ContactName")
End Sub
Sub GetDataSet()
If m_DAL Is Nothing Then
m_DAL = New DataAccess
End If
dsSupplier = m_DAL.CreateDataSet()
dtSupplier = dsSupplier.Tables("Supplier")
dvSupplier = dtSupplier.DefaultView
End Sub
Private Sub btnSave_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSave.Click
Dim m_DAL As New DataAccess
Me.BindingContext(dsSupplier.Tables
("Supplier")).EndCurrentEdit()
m_DAL.UpdateDataSet(dsSupplier.GetChanges())
End Sub
End Class
missing. I am having a problem with getting the Textbox
value to update the second time. For example, I change
the value of the textbox two times for whatever reason.
I get an error message of "Concurrency violation: the
UpdateCommand affected 0 records." on the second change.
Why am I getting that error message? Is this a good
example of a DAL? If it is not where could I find one?
Any kind of help will be great.
I have a class (DataAccess) that I am calling from a
Windows Form. The class is connecting to the NorthWind
database using the Suppliers table. The Windows Form has
a Textbox (ContactName) and a Button (Save).
My code below is based off from Microsoft's "N-Tier Data
Form and Data Layer" example.
Thanks in Advance
Charles
--DataAccess Class--
Option Strict On
Imports System.Data.SqlClient
Public Class DataAccess
Protected strConn As String
= "server=localhost;database=northwind;integrated
security=sspi"
Protected DidPreviouslyConnect As Boolean = False
Private daSuppliers As SqlDataAdapter
Public Function CreateDataSet() As DataSet
Dim dsSupplier As DataSet
Dim IsConnecting As Boolean = True
While IsConnecting
Try
Dim SQLConn As New SqlConnection(strConn)
Dim strSQL As String = "SELECT * FROM
Suppliers"
Dim Cmd As New SqlCommand(strSQL, SQLConn)
daSuppliers = New SqlDataAdapter(Cmd)
Dim CmdBuild As New SqlCommandBuilder
(daSuppliers)
dsSupplier = New DataSet
daSuppliers.Fill(dsSupplier, "Supplier")
IsConnecting = False
DidPreviouslyConnect = True
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return dsSupplier
End While
End Function
Public Sub UpdateDataSet(ByVal inDS As DataSet)
If inDS Is Nothing Then
Exit Sub
End If
Try
If (Me.daSuppliers Is Nothing) Then
CreateDataSet()
End If
inDS.EnforceConstraints = False
daSuppliers.Update(inDS, "Supplier")
Catch exc As Exception
Debug.WriteLine(exc.Message)
End Try
End Sub
End Class
--Form1 Class--
Option Strict On
Imports System.Data.SqlClient
Imports DataAccessLayertrl1
Public Class Form1
Inherits System.Windows.Forms.Form
Protected DidPreviouslyConnect As Boolean = False
Private dsSupplier As DataSet
Private dtSupplier As DataTable
Private dvSupplier As DataView
Dim m_DAL As DataAccess
'#Region " Windows Form Designer generated code " Here
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
GetDataSet()
DataBindControls()
End Sub
Sub DataBindControls()
txtContactName.DataBindings.Add("Text",
dvSupplier, "ContactName")
End Sub
Sub GetDataSet()
If m_DAL Is Nothing Then
m_DAL = New DataAccess
End If
dsSupplier = m_DAL.CreateDataSet()
dtSupplier = dsSupplier.Tables("Supplier")
dvSupplier = dtSupplier.DefaultView
End Sub
Private Sub btnSave_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSave.Click
Dim m_DAL As New DataAccess
Me.BindingContext(dsSupplier.Tables
("Supplier")).EndCurrentEdit()
m_DAL.UpdateDataSet(dsSupplier.GetChanges())
End Sub
End Class