I'm building a simple WPF application using LINQ to SQL to retrieve data from a SQL Server 2008 database and list them in a listbox.
My database has Customer table with
CustomerId
and
Name
columns. I used O/R designer to build their datacontext class.
I simply have a text box and a button which, when clicked, adds a customer's name provided in the textbox to both the database and update the listbox.
This app work successfully for one user. And here is the code behind (which I modified from this example
Simple Demo of Binding to a Database in WPF using LINQ-SQL:-
imports System.Collections.ObjectModel
Class Window1
Private Shared _db As New DataClasses1DataContext()
Private _knownCustomer As ObservableCustomer
Public Sub New()
InitializeComponent()
End Sub
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
_knownCustomer = New ObservableCustomer(_db)
Me.ListBox1.ItemsSource = _knownCustomer
End Sub
Private Sub add_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Button1.Click
Dim newCustomer As New Customer()
newCustomer.Name = TextBox1.Text
_db.Customers.InsertOnSubmit(newCustomer)
_db.SubmitChanges()
TextBox1.Text = ""
_knownCustomer.Add(newCustomer)
End Sub
End Class
Public Class ObservableCustomer
Inherits ObservableCollection(Of Customer)
Public Sub New(ByVal db As DataClasses1DataContext)
For Each thisCustomer As Customer In db.Customers
Me.Add(thisCustomer)
Next
End Sub
End Class
My problem is, if user1 and user2 are both trying to add a new customer name, say, user1 wants to add "Simon" and then user2 want to add "Micheal", is it possible for user2 (when click "add") to see both "Simon" (added by user1) and also "Michael" (that is added by him) in his listbox? I mean the last user will get updated data from database, not their local observable collection when they click add button.
Sorry for poor english but I hope this is understandable and please help. X|