Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C# ASP.NET SQL
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 5-Feb-13 22:28pm
Edited 5-Feb-13 22:33pm
v2

1 solution

Rate this: bad
good
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")
   }
else
   {
     // success
     Messagebox.Show("Record Inserted Successfully")
   }
Happy Coding!
Smile | :)
  Permalink  
v2
Comments
gaurish thakkar at 6-Feb-13 5:17am
   
What should be the code to notify the user on another screen
Aarti Meswania at 6-Feb-13 5:24am
   
use messagebox,
see updated solution
gaurish thakkar at 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 at 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
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
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