Hi There;

I have an application that work in multi-user environment. Would like to know what are the possible way to update the datagridview in other user workstation once someone did the changes?

For instance, when user A had done some Add/Update/delete for a record, datagridview that work by user A can be reloaded to reflect the changes, but what i can do so that other user that work on same appliation to get the datagridview refresh/reload automatically?

Note: Application work in C# (Window Form) and SQL server 2005 as backend

Thank you...

>Update Datagridview in multi-user environment

Refresh the DataSource.

i refresh datasource after one user updated his work. but before the refresh , if other user already open the datasource in his pc, his datasource will not be updated. because update will happen in other users who open after the refresh. So some user who open just before refresh they saw old db.

i m not sure i can tell exactly the situation, but i m in this type of trouble, if anyone have idea that would help.

Is this a client-server type application, or is it simply multiple instances of an application independently operating on users' workstations? In the case of the former, you can try using event notifications. When one user updates the data, an event is fired and the other clients refresh the data as necessary. In the case of the latter, you'd really just need to have a routine that periodically checks for updates. It would not be instant notification, but it could be as close to it is you program it to be.

it is simply multiple instances of an application like a bus ticket management, when 2 ticket counter can sold same ticket at a time.
i use several checking to prevent this, and this was successful but my clients r complaining that it is slow for these steps.

@apegram u say "one user updates the data, an event is fired and the other clients refresh the data as necessary" i think that could work, thanks.

Hi apegram.. which event could be use to trigger the changes?

How could that be done? Sorry, being new to this...

Hi apegram.. which event could be use to trigger the changes?

How could that be done? Sorry, being new to this...

I'm sorry, but I do not have an exact implementation in mind. I was speaking more in theoretical terms. If you were to have a client/server set-up with your multi-user data application, you could create an event for situations where the database has been updated. This would be an event at the server. When a client commits changes to the data, you would programmatically raise the event. The other clients would need to subscribe to it in order to be notified, and which point they could take the necessary steps of refreshing their own views of the data.

I do not work in client-server Winform scenarios, I work mostly in ASP.NET. Perhaps someone who does more Winform development could shed a little more light on it or provide an idea for an alternate event-model or even a different implementation suggestion altogether.

i used 2 way
1. a timer set up which periodically update the datasource
2.i use a staic value when u user update it will change , and since is value is checking by a timer it create refresh of db and go prev state.

but both of the way is looking bad, because it slowdown the server.
i want a better way.

Hi..

I found some sample code from Microsoft support site, below are those code adapted from the site.

Looking at the code, it's seem work ok. Whenever i did the changes in the table (at DB), it does reflected the changes in the datagrideview, however, it's keep on refresh the datagridview for every second which sholdn't be the case. It should only refresh the datagridview when there is some changes... anything that i had oversee?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
 
namespace SQLNotificationTest 
{ 
    public partial class Form1 : Form 
    { 
          
        private DataSet myDataSet = null; 
        private SqlConnection connection = null; 
        private SqlCommand command = null; 
        private string connstr; 
 
 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private bool EnoughPermission() 
        { 
            SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted); 
            try 
            { 
                perm.Demand(); 
                return true; 
            } 
            catch (System.Exception) 
            { 
                return false; 
            } 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
 
            // Remove any existing dependency connection, then create a new one. 
            connstr = "Data Source=Test-pc;Integrated Security=True;Initial Catalog=Beauty;"; 
            string ssql = "select * from CustomerDetails"; 
 
            SqlDependency.Stop(connstr); 
            SqlDependency.Start(connstr); 
            if (connection == null) 
            { connection = new SqlConnection(connstr); } 
            if (command == null) 
            { command = new SqlCommand(ssql, connection); } 
            if (myDataSet == null) 
            { myDataSet = new DataSet();} 
            GetBeautyData(); 
        } 
 
        private void GetBeautyData() 
        { 
            myDataSet.Clear(); 
            // Ensure the command object does not have a notification object. 
            command.Notification = null; 
            // Create and bind the SqlDependency object to the command object. 
            SqlDependency dependency = new SqlDependency(command); 
            dependency.onchange += new onchangeEventHandler(dependency_onchange); 
 
            using (SqlDataAdapter adapter = new SqlDataAdapter(command)) 
            { 
                adapter.Fill(myDataSet, "CustomerDetails"); 
                dataGridView1.DataSource = myDataSet; 
                dataGridView1.DataMember = "CustomerDetails"; 
            } 
        } 
 
 
    delegate void UIDelegate(); 
       private void dependency_onchange(object sender, SqlNotificationEventArgs e) 
        { 
            UIDelegate uidel = new UIDelegate(RefreshData); 
            this.Invoke(uidel, null); 
 
            //Remove the handler as it is used for a single notification. 
            SqlDependency dependency =(SqlDependency)sender; 
            dependency.onchange -= dependency_onchange; 
        } 
        
        private void RefreshData() 
        { 
            // Since the code is executing on the UI thread,it is safe to update the UI. 
 
            label1.Text = "Database had some changes and are applied in the Grid"; 
 
            // Reload the dataset that is bound to the grid. 
            GetBeautyData(); 
        } 
 
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
            SqlDependency.Stop(connstr); 
            if (connection != null) 
                connection.Close(); 
    } 
   
    } 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.