Click here to Skip to main content
13,097,012 members (81,701 online)
Rate this:
Please Sign up or sign in to vote.
I am trying to implement a feature where in One user is notified on the change in the database when another user modifies the record .

The reference article i took is
Query Notification using SqlDependency and SqlCacheDependency[^]

protected void btn_comment_Click(object sender, EventArgs e)
            //if (!CheckUserPermissions())
            DateTime dtt = DateTime.Now;
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["legup"].ConnectionString;
            string queue = "SendQueue";

                SqlCommand cmd1 = new SqlCommand();
                cmd1.Connection = cn;
              // cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.CommandText = "Insert into [legup].[dbo].[legup_comments](project_id,user_id,comments,date)	Values (3,1,'" + txt_comments.Text + "','" + dtt + "')";
                   ////cmd1.CommandText = "sp_insert_comments";
                cmd1.Notification = null;
                    //cmd1.Parameters.AddWithValue("@proj_id", 3);
                    //cmd1.Parameters.AddWithValue("@user_id", 1);
                    //cmd1.Parameters.AddWithValue("@comments", txt_comments.Text);
                    //cmd1.Parameters.AddWithValue("@dt", dtt);

                    SqlDependency dependency = new SqlDependency(cmd1);
                    dependency.OnChange += new OnChangeEventHandler(OnDataInsert);
                    txt_comments.Text = String.Empty;
               Response.Write("failed to start");
        catch (Exception ex)
            Response.Write("<br>DFGH:: " + ex.Message);
           // SqlDependency.Stop(str_con);
            if (cn != null)

    void OnDataInsert(object sender, SqlNotificationEventArgs e)
        SqlDependency dependency = sender as SqlDependency;
        Response.Write("sucess on data insert");
        // Notices are only a one shot deal
        // so remove the existing one so a new 
        // one can be added
        dependency.OnChange -= new OnChangeEventHandler(OnDataInsert);
        // Fire the event
        Response.Write("<br> Fire the event");
        //    if (OnNewMessage != null)
        //    {
        //        OnNewMessage();
        //    }

How can i access the recently inserted record from another web forms
Or how can i get notified on the insert query execution
Posted 5-Feb-13 21:28pm
Updated 5-Feb-13 21:33pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

this way...
int indicate = cmd1.ExecuteNonQuery();
if (indicate==0)
     // fail
     Messagebox.Show("Record Insertion failed")
     // success
     Messagebox.Show("Record Inserted Successfully")

Happy Coding!
gaurish thakkar 6-Feb-13 5:17am
What should be the code to notify the user on another screen
Aarti Meswania 6-Feb-13 5:24am
use messagebox,
see updated solution
gaurish thakkar 6-Feb-13 5:28am
That will show message to the same user. Can i show the message to another user like Facebook notifications wherein the latest changes and updates of the user are shown as popup to another user
Aarti Meswania 6-Feb-13 5:31am
then you should maintain track of user activity with status read unread for other user of your application

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100