Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
    {
        try
        {

            //if (!CheckUserPermissions())
            //  
           
            DateTime dtt = DateTime.Now;
            dtt.Date.ToString("MM/dd/yyyy");
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["legup"].ConnectionString;
            string queue = "SendQueue";



           SqlDependency.Stop(str_con);
           if(SqlDependency.Start(str_con))
            {
               
                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);

                    cn.Open();
                    cmd1.ExecuteNonQuery();

                    txt_comments.Text = String.Empty;
                    display();
               

            }
           else
           {
               Response.Write("failed to start");
           }

        }
        catch (Exception ex)
        {
            Response.Write("<br>DFGH:: " + ex.Message);
        }
        finally
        {
           // SqlDependency.Stop(str_con);
            if (cn != null)
                cn.Close();

        }

    }


    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
Updated 5-Feb-13 21:33pm
v2

1 solution

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

Happy Coding!
:)
 
Share this answer
 
v2
Comments
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)



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