Click here to Skip to main content
15,891,253 members
Home / Discussions / C#
   

C#

 
GeneralRe: Could not cal the repaint usercontrol?? Pin
Sarita S31-Mar-15 0:21
Sarita S31-Mar-15 0:21 
GeneralRe: Could not cal the repaint usercontrol?? Pin
OriginalGriff31-Mar-15 0:22
mveOriginalGriff31-Mar-15 0:22 
GeneralRe: Could not cal the repaint usercontrol?? Pin
Sarita S31-Mar-15 0:49
Sarita S31-Mar-15 0:49 
GeneralRe: Could not cal the repaint usercontrol?? Pin
OriginalGriff31-Mar-15 0:55
mveOriginalGriff31-Mar-15 0:55 
QuestionHow to have a Select Query like Sql Server Cross Apply query in MS-Access? Pin
Paramu197330-Mar-15 16:05
Paramu197330-Mar-15 16:05 
AnswerRe: How to have a Select Query like Sql Server Cross Apply query in MS-Access? Pin
Afzaal Ahmad Zeeshan30-Mar-15 16:20
professionalAfzaal Ahmad Zeeshan30-Mar-15 16:20 
Questionhow get array of drawing in inkcanvas in wpf Pin
Member 1111269830-Mar-15 9:24
Member 1111269830-Mar-15 9:24 
QuestionHow to handle SQL Dependency error properly Pin
Tridip Bhattacharjee30-Mar-15 2:35
professionalTridip Bhattacharjee30-Mar-15 2:35 
i am monitoring a database table for data change like insert/Update. some sql dependency throw error i heard like when no activity is there for a specific table for long days like 3/4 days. i am trying to say if i am monitoring a table and if no data inserted or updated in that table for long time then SQL Dependency throw error. here is my code. please see my code and tell me am i handing the error properly.

my sql dependency code is written in windows service. i want if any error is thrown then my windows service should restart. here is my full code

public partial class PartIndexer : ServiceBase
{
static string connectionString = "server=222;uid=222;password=222;database=wwww;Pooling=true;Connect Timeout=20;";
SqlDependency dep;

public PartIndexer()
{
InitializeComponent();
}

#region OnStart
protected override void OnStart(string[] args)
{
RegisterNotification();
MailNotify("STARTED");
}
#endregion

#region RegisterNotification
///
/// RegisterNotification
/// this is main routine which will monitor data change in ContentChangeLog table
///

private void RegisterNotification()
{
string tmpdata = "";
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 ActivityDate FROM [bba-reman].MyLog";
dep = new SqlDependency(cmd);
dep.OnChange += new OnChangeEventHandler(OnDataChange);
SqlDataReader dr = cmd.ExecuteReader();
{
while (dr.Read())
{
if (dr[0] != DBNull.Value)
{
tmpdata = dr[0].ToString();
}
}
}
dr.Dispose();
cmd.Dispose();
}
}
finally
{
//SqlDependency.Stop(connStr);
}

}
#endregion

// this below function never used
public void ReStartService()
{
ServiceController service = new ServiceController("PartIndexer");

if ((service.Status.Equals(ServiceControllerStatus.Stopped)) || (service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Start();
}
else
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
}

#region OnDataChange
///
/// OnDataChange
/// OnDataChange will fire when after data change found in ContentChangeLog table
///

/// <param name="sender" />
/// <param name="e" />
void OnDataChange(object sender, SqlNotificationEventArgs e)
{
((SqlDependency)sender).OnChange -= OnDataChange;

if (e.Source == SqlNotificationSource.Timeout)
{
var template = new MailTemplate()
.WithBody("HI,

Part Indexer Service Exception Timeout occur " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Service Exception Timeout occur")
.WithSender("xxx@xxx.com")
.WithRecepient("xxx@xxx.com")
.Send();
Environment.Exit(1);
return;
}
else if (e.Source != SqlNotificationSource.Data)
{
var template = new MailTemplate()
.WithBody("HI,

Part Indexer Service Exception SqlNotificationSource.Data " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Service Exception SqlNotificationSource.Data")
.WithSender("xxx@xxx.com")
.WithRecepient("xxx@xxx.com")
.Send();

Environment.Exit(1);
}

StartIndex();
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()
{
//eventLog1.WriteEntry("Web Service called start for indexing data");

PartIndexerWS.AuthHeader oAuth = new PartIndexerWS.AuthHeader();
oAuth.Username = "Admin";
oAuth.Password = "Admin";

PartIndexerWS.SearchDataIndex DataIndex = new PartIndexerWS.SearchDataIndex();
DataIndex.AuthHeaderValue = oAuth;
try
{
DataIndex.StartIndex();
//eventLog1.WriteEntry("Web Service called stop for indexing data");
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message.ToString());
//eventLog1.WriteEntry("Web Service call error "+ex.Message.ToString());

}

}
#endregion

#region MailNotify
///
/// MailNotify
/// fire mail when apps start & exit
///

/// <param name="strStatus" />
void MailNotify(string strStatus)
{
if (strStatus == "STARTED")
{
var template = new MailTemplate()
.WithBody("HI,

Part Indexer Started Date " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Started")
.WithSender("xxx@xxx.com")
.WithRecepient("xxx@xxx.com")
.Send();
//eventLog1.WriteEntry("mail fired ");

}
else if (strStatus == "STOPPED")
{
var template = new MailTemplate()
.WithBody("HI,

Part Indexer stopped Date " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Stopped")
.WithSender("xxx@xxx.com")
.WithRecepient("xxx@xxx.com")
.Send();
//eventLog1.WriteEntry("mail fired ");
}
}
#endregion

#region OnStop
protected override void OnStop()
{
System.Data.SqlClient.SqlDependency.Stop(connectionString);
MailNotify("STOPPED");
//eventLog1.WriteEntry("Part Indexer stopped Date : " + DateTime.Now.ToLongDateString());

}
#endregion
}

see this function because if error occur then i am restarting service from here by this code Environment.Exit(1);.

void OnDataChange(object sender, SqlNotificationEventArgs e)
{
((SqlDependency)sender).OnChange -= OnDataChange;

if (e.Source == SqlNotificationSource.Timeout)
{
var template = new MailTemplate()
.WithBody("HI,

Part Indexer Service Exception Timeout occur " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Service Exception Timeout occur")
.WithSender("xxx@xxx.com")
.WithRecepient("xxx@xxx.com")
.Send();
Environment.Exit(1);
return;
}
else if (e.Source != SqlNotificationSource.Data)
{
var template = new MailTemplate()
.WithBody("HI,

Part Indexer Service Exception SqlNotificationSource.Data " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Service Exception SqlNotificationSource.Data")
.WithSender("xxx@xxx.com")
.WithRecepient("xxx@xxx.com")
.Send();

Environment.Exit(1);
}

StartIndex();
RegisterNotification();
}

the problem is my service is not sending any mail when data change in specific table but if i restart my service then service is sending mail when data is changed in specific table which is monitoring by the sql dependency. need help.
tbhattacharjee

AnswerRe: How to handle SQL Dependency error properly Pin
Eddy Vluggen30-Mar-15 2:49
professionalEddy Vluggen30-Mar-15 2:49 
GeneralRe: How to handle SQL Dependency error properly Pin
Mycroft Holmes30-Mar-15 12:31
professionalMycroft Holmes30-Mar-15 12:31 
Questionwhen people draw UML Sequence diagrams and when people draw UML Activity diagrams Pin
Tridip Bhattacharjee30-Mar-15 0:34
professionalTridip Bhattacharjee30-Mar-15 0:34 
AnswerRe: when people draw UML Sequence diagrams and when people draw UML Activity diagrams Pin
OriginalGriff30-Mar-15 0:43
mveOriginalGriff30-Mar-15 0:43 
AnswerRe: when people draw UML Sequence diagrams and when people draw UML Activity diagrams PinPopular
Pete O'Hanlon30-Mar-15 0:53
mvePete O'Hanlon30-Mar-15 0:53 
GeneralRe: when people draw UML Sequence diagrams and when people draw UML Activity diagrams Pin
OriginalGriff30-Mar-15 2:13
mveOriginalGriff30-Mar-15 2:13 
AnswerRe: when people draw UML Sequence diagrams and when people draw UML Activity diagrams Pin
Eddy Vluggen30-Mar-15 0:59
professionalEddy Vluggen30-Mar-15 0:59 
AnswerRe: when people draw UML Sequence diagrams and when people draw UML Activity diagrams Pin
Gerry Schmitz30-Mar-15 11:14
mveGerry Schmitz30-Mar-15 11:14 
QuestionAsync Question Pin
Kevin Marois29-Mar-15 7:36
professionalKevin Marois29-Mar-15 7:36 
AnswerRe: Async Question Pin
Dave Kreskowiak29-Mar-15 7:58
mveDave Kreskowiak29-Mar-15 7:58 
QuestionInstall Windows Service Pin
Kevin Marois29-Mar-15 5:38
professionalKevin Marois29-Mar-15 5:38 
AnswerRe: Install Windows Service Pin
Richard Andrew x6429-Mar-15 5:51
professionalRichard Andrew x6429-Mar-15 5:51 
GeneralRe: Install Windows Service Pin
Kevin Marois29-Mar-15 6:55
professionalKevin Marois29-Mar-15 6:55 
GeneralRe: Install Windows Service Pin
Richard Andrew x6429-Mar-15 7:21
professionalRichard Andrew x6429-Mar-15 7:21 
GeneralRe: Install Windows Service Pin
Dave Kreskowiak29-Mar-15 7:51
mveDave Kreskowiak29-Mar-15 7:51 
GeneralRe: Install Windows Service Pin
Kevin Marois29-Mar-15 10:30
professionalKevin Marois29-Mar-15 10:30 
AnswerRe: Install Windows Service Pin
Richard Andrew x6429-Mar-15 15:00
professionalRichard Andrew x6429-Mar-15 15:00 

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.