Click here to Skip to main content
15,900,378 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Datepicker value not saved in MySql database Pin
Gerry Schmitz28-Apr-15 9:14
mveGerry Schmitz28-Apr-15 9:14 
QuestionWhat is Constructor Overloading in C# .net ? Pin
dhivya.sakthi26-Apr-15 20:35
dhivya.sakthi26-Apr-15 20:35 
AnswerRe: What is Constructor Overloading in C# .net ? Pin
OriginalGriff26-Apr-15 21:28
mveOriginalGriff26-Apr-15 21:28 
AnswerRe: What is Constructor Overloading in C# .net ? Pin
Dr Gadgit27-Apr-15 3:43
Dr Gadgit27-Apr-15 3:43 
AnswerRe: What is Constructor Overloading in C# .net ? Pin
David A. Gray3-May-15 10:19
David A. Gray3-May-15 10:19 
QuestionVideo cutter / merger in C# winfrom Pin
Member 1162007426-Apr-15 7:35
Member 1162007426-Apr-15 7:35 
GeneralRe: Video cutter / merger in C# winfrom Pin
Sascha Lefèvre26-Apr-15 7:46
professionalSascha Lefèvre26-Apr-15 7:46 
QuestionSql Dependency onchange event not firing every time c# Pin
Tridip Bhattacharjee25-Apr-15 9:56
professionalTridip Bhattacharjee25-Apr-15 9:56 
i have implemented sql dependency in windows service. when data will be changed in table then onchange event will fire and from there i am invoking a web service.

i will share my full code. i tested many time in my pc before installing the window service production pc. i works. suppose if i install in production pc today then it works for today but when i test next day then i was onchange event not firing.

so i found onchange event firing only first day and from the next day onchange event not firing. may be i made some mistake in code. so it is my request please some one see my code in details and help me where i made the mistake for which it is not working properly.
C#
public partial class PartIndexer : ServiceBase
{
    static string connectionString = "server=xxx;uid=xxx;password=xxx;database=xxx;Pooling=true;Connect Timeout=20;";
    SqlDependency dep;

    public PartIndexer()
    {
        InitializeComponent();
    }

    private string GetLoggedInUser()
    {
        string userName = "";
        if (System.Security.Principal.WindowsIdentity.GetCurrent() != null)
        {
            userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        }
        return userName;
    }

    #region OnStart
    protected override void OnStart(string[] args)
    {
        BBALogger.Write("PartIndexer Service OnStart called start", BBALogger.MsgType.Info);
        RegisterNotification();
        MailSend(); // notification mail send
        BBALogger.Write("PartIndexer Service OnStart called end, logged in user " + GetLoggedInUser(), BBALogger.MsgType.Info);
    }
    #endregion

    #region RegisterNotification
    /// 
    /// RegisterNotification
    /// this is main routine which will monitor data change in ContentChangeLog table
    /// 
    private void RegisterNotification()
    {
        string tmpdata = "";
        BBALogger.Write("PartIndexer Service RegisterNotification called start", BBALogger.MsgType.Info);

        System.Data.SqlClient.SqlDependency.Stop(connectionString);
        System.Data.SqlClient.SqlDependency.Start(connectionString);

        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT TestTable FROM [dbo].ContentChangeLog";
                dep = new SqlDependency(cmd);
                dep.OnChange += new OnChangeEventHandler(OnDataChange);
                SqlDataReader dr = cmd.ExecuteReader();
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        tmpdata = dr[0].ToString();
                    }
                }
                dr.Dispose();
                cmd.Dispose();
            }
        }
        catch (Exception ex)
        {
            BBALogger.Write("PartIndexer Service RegisterNotification Error "+ex.Message.ToString(), BBALogger.MsgType.Error);
        }
        finally
        {
            BBALogger.Write("PartIndexer Service RegisterNotification called end", BBALogger.MsgType.Info);

        }

    }
    #endregion

    #region OnDataChange
    /// 
    /// OnDataChange
    /// OnDataChange will fire when after data change found in ContentChangeLog table
    /// 
    /// 
    /// 
    void OnDataChange(object sender, SqlNotificationEventArgs e)
    {
        ((SqlDependency)sender).OnChange -= OnDataChange;

        BBALogger.Write("PartIndexer Service RegisterNotification called end", BBALogger.MsgType.Info);

        if (e.Source == SqlNotificationSource.Timeout)
        {
    MailSend(); // notification mail send
    BBALogger.Write("PartIndexer Service SqlNotificationSource.Timeout error", BBALogger.MsgType.Error);

            Environment.Exit(1);
        }
        else if (e.Source != SqlNotificationSource.Data)
        {
        MailSend(); // notification mail send
            BBALogger.Write("PartIndexer Service SqlNotificationSource.Data", BBALogger.MsgType.Error);

            Environment.Exit(1);
        }
        else if (e.Type == SqlNotificationType.Change)
        {
            StartIndex();
            BBALogger.Write("PartIndexer Service Data changed", BBALogger.MsgType.Info);
        }
        else
        {
            BBALogger.Write(string.Format("Ignored change notification {0}/{1} ({2})", e.Type, e.Info, e.Source), BBALogger.MsgType.Warnings);
        }

        RegisterNotification();
    }
     #endregion

    #region StartIndex
    /// 
    /// StartIndex
    /// this routine will call web service in bba reman website which will invoke routine to re-index data
    /// 
    void StartIndex()
    {
        // calling web service if change is notified

    }
    #endregion

    #region MailSend
    /// 
    /// MailNotify
    /// fire mail when apps start & exit
    /// 
    /// 
    void MailSend()
    {
    // mail send code
    }
    #endregion

    #region OnStop
    protected override void OnStop()
    {
        BBALogger.Write("PartIndexer Service StartIndex called end, logged in user " + GetLoggedInUser(), BBALogger.MsgType.Info);
        System.Data.SqlClient.SqlDependency.Stop(connectionString);
        MailNotify("STOPPED");
    }
    #endregion
}

another issue i noticed that when i start my windows service and leave it run in one day when i try to stop or restart the the windows service then i saw windows can not stop the service. it means definitely there is some flaw in my code which i am not being able to point out. so please help me. thanks
tbhattacharjee

AnswerRe: Sql Dependency onchange event not firing every time c# Pin
Dave Kreskowiak25-Apr-15 10:55
mveDave Kreskowiak25-Apr-15 10:55 
Questionc# opening database to array Pin
ruspj25-Apr-15 8:03
ruspj25-Apr-15 8:03 
AnswerRe: c# opening database to array Pin
Sascha Lefèvre25-Apr-15 8:16
professionalSascha Lefèvre25-Apr-15 8:16 
GeneralRe: c# opening database to array Pin
ruspj25-Apr-15 8:31
ruspj25-Apr-15 8:31 
GeneralRe: c# opening database to array Pin
Sascha Lefèvre25-Apr-15 8:50
professionalSascha Lefèvre25-Apr-15 8:50 
GeneralSharpDevolp 4.3 Pin
Member 465445225-Apr-15 3:41
Member 465445225-Apr-15 3:41 
GeneralRe: SharpDevolp 4.3 Pin
Eddy Vluggen25-Apr-15 7:32
professionalEddy Vluggen25-Apr-15 7:32 
QuestionRegistering SQL Dependency again from onchange event Pin
Tridip Bhattacharjee24-Apr-15 4:48
professionalTridip Bhattacharjee24-Apr-15 4:48 
QuestionSql Dependency onchange event not firing every time c# Pin
Tridip Bhattacharjee24-Apr-15 3:48
professionalTridip Bhattacharjee24-Apr-15 3:48 
QuestionUnable to connect Oracle database from C# Pin
meeram3924-Apr-15 3:47
professionalmeeram3924-Apr-15 3:47 
AnswerRe: Unable to connect Oracle database from C# Pin
Dave Kreskowiak24-Apr-15 4:03
mveDave Kreskowiak24-Apr-15 4:03 
GeneralRe: Unable to connect Oracle database from C# Pin
meeram3924-Apr-15 14:53
professionalmeeram3924-Apr-15 14:53 
GeneralRe: Unable to connect Oracle database from C# Pin
Dave Kreskowiak24-Apr-15 17:21
mveDave Kreskowiak24-Apr-15 17:21 
GeneralRe: Unable to connect Oracle database from C# Pin
meeram3924-Apr-15 19:28
professionalmeeram3924-Apr-15 19:28 
GeneralRe: Unable to connect Oracle database from C# Pin
meeram3925-Apr-15 0:39
professionalmeeram3925-Apr-15 0:39 
GeneralRe: Unable to connect Oracle database from C# Pin
Dave Kreskowiak25-Apr-15 3:42
mveDave Kreskowiak25-Apr-15 3:42 
GeneralRe: Unable to connect Oracle database from C# Pin
meeram3925-Apr-15 5:46
professionalmeeram3925-Apr-15 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.