Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:-
VB
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|
Posted
Updated 2-Feb-10 21:39pm
v3

1 solution

How are the applications connected ? Is it a website or a client side app ? Either way, if there's a central data store, all this LINQ code is utterly irrelevant. What's needed is some sort of timer or messenger to tell the app to update based on changes made to the data store by another user.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900