Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
i have developed c# application that connects to sql server
my sql server database contains about 20 tables.
a user(1) asks for table records , the application connects to the server and sql command is executed to fill the data into gridview
the problem is when another user(2) updated the same table that was required by user(1) , the changes wont reflect to user(1) gridview.

thanks for your reply in Advance.

What I have tried:

i have tried c# sqldependency class using service broker but that didn't much helped me out because sqldependency is used to monitor a single sql command
what i want is to monitor any changes happened to the table and notify all users to refresh there datasource and refill the datagridview

part of the code i used :

starting sql dependency :

SqlDependency depend = new SqlDependency(cmd);
            depend.OnChange += new OnChangeEventHandler(OnDependencyChange);
            cmd.Notification = null;
            SqlDependency.Start(sqlcon.ConnectionString.ToString());


OnDependencyChange :

void OnDependencyChange(object sender, SqlNotificationEventArgs e)
        {
            SqlDependency dependency = sender as SqlDependency;
            dependency.OnChange -= new OnChangeEventHandler(OnDependencyChange);
            MessageBox.Show("table is altered");
        }
Posted
Updated 7-Mar-17 2:29am

1 solution

You have to set it up yourself. One way would be to have an audit table that stores info regarding changes to a table. I would probably go with the following schema:

LastChanged datetime
TableName nvarchar(64)

and write stored proc that returns the count of records that were added for a specified table after a specified datetime. If the count is 0, the app doesn't need to do anything. If the count is >= 1, do something in the app.
 
Share this answer
 
Comments
MHD Salim Al-Tarsha 11-Mar-17 6:55am    
thanks for your answer , it's really helpful
but i guess I'm gonna use database locks
when i want to update a record it's locked until i finish editing
again , thanks

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