Page_Load firing multiple times? - c#

We have been dealing with an error for the last couple of days, so we created a small page (quick and dirty programming, my apologies in advance) that connects to the database, checks if a document exists, and displays some data related to the document. If there is an exception, an email is sent with the exception information and some log data.
Here's a simplified version of the code (short explanation below):
namespace My.Namespace
{
public partial class myClass : System.Web.UI.Page
{
private static SqlConnection conn = null;
private static SqlCommand command1 = null;
private static SqlCommand command2 = null;
private static string log = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
log += "START\n";
string docId = Request.QueryString["docId"];
if (!String.IsNullOrEmpty(docName))
{
bool docExists = doesDocExist(docId);
if (docExists == true)
{
string docMetadata = getMetadata(docId);
Response.Write(docMetadata);
}
}
else
{
// display error message
}
}
catch (sqlException sqlex)
{
// process exception
sendErrorMessage(sqlex.Message);
}
catch (Exception ex)
{
// process exception
sendErrorMessage(ex.Message);
}
}
}
private static bool doesDocExist(string docId)
{
log += "In doesDocExist\n";
bool docExists = false;
try
{
// open db connection (conn)
string cmd = String.Format("SELECT COUNT(*) FROM docs WHERE id='{0}'", docId);
command1 = new SqlCommand(cmd, conn);
conn.Open();
var val = command1.ExecuteScalar();
int numberOfRows = int.Parse(val.ToString());
if (numberOfRows > 0) { docExists = true; }
}
finally
{
// close db connection (conn)
}
return docExists;
}
protected string getMetadata(string docId)
{
log += "In getMetadata\n";
string docMetadata = "";
try
{
// open db connection (conn)
string cmd = String.Format("SELECT metadata FROM docs WHERE id='{0}'", docID);
command2 = new SqlCommand(cmd, conn);
conn.Open();
SqlDataReader rReader = command2.ExecuteReader();
if (rReader.HasRows)
{
while (rReader.Read())
{
// process metadata
docMetadata += DOCMETADATA;
}
}
}
return docMetadata;
}
public static void sendErrorMessage(string messageText)
{
HttpContext.Current.Response.Write(messageText);
// Send string log via email
}
}
}
I know it's too long, so here is a quick description of it. We have a class with the Page_Load method and three other methods:
doesDocExists: returns a bool value indicating if an document ID is in the database.
getMetadata: returns a string with metadata related to the document.
sendErrorMessage: sends an email with a log generated during the page.
From Page_Load we call doesDocExists. If the value returned is true, then it calls getMetadata and displays the value on the screen. If there's any error, it is caught in the Page_Load and sent as an email.
The problem is that when there's an error, instead of getting an email with the log (i.e.: START - In Function1 - In Function2), the log appears 100 times in the email (i.e.: START - In Function1 - In Function2 - Start - In Function1 - In Function2 - START... and so on), as if Page_Load was fired that many times.
We read online (http://www.craigwardman.com/blog/index.php/2009/01/asp-net-multiple-page-load-problem/) that it could be because of the PostBack. So, we added the condition if (!Page.IsPostBack), but the result is still the same.
Is there any reason why Page_Load would be triggered multiple times? Or is it that we are doing something wrong with the log variable and/or the try/catch that causes this behavior?

The log may be long because you are declaring the string log as static. Does it need to be static?

private static SqlConnection conn = null;
private static SqlCommand command1 = null;
private static SqlCommand command2 = null;
private static string log = "";
The problem is that log is Singleton along with other properties.
Whenever you access that page, you append text to log property which ends up being START - In Function1 - In Function2 - Start - In Function1 - In Function2 - START... and so on
Base on your scenario, you do not need to use Singleton inside myClass.
FYI: Since I do not know the rest of your code, ensure to instantiate conn, command1, command2.

If your page load functions are execute twice because post back is possible when you clicking on the button or link, so should check it and run by the below
if (!IsPostBack)
{
try
{
log += "START\n";
string docId = Request.QueryString["docId"];
if (!String.IsNullOrEmpty(docName))
{
bool docExists = doesDocExist(docId);
if (docExists == true)
{
string docMetadata = getMetadata(docId);
Response.Write(docMetadata);
}
}
else
{
// display error message
}
}
catch (sqlException sqlex)
{
// process exception
sendErrorMessage(sqlex.Message);
}
catch (Exception ex)
{
// process exception
sendErrorMessage(ex.Message);
}
}
}

Related

C# WinUI 3 Desktop Application Issue with ContentDialog and the XamlRoot

I am building a C# WinUI 3 desktop app running on Windows 10. I want to use a contentdialog to display error messages.
I am calling this method from multiple catch clauses:
private async void DisplayErrorDialog(string content)
{
ContentDialog ErrorDialog = new ContentDialog()
{
Title = "* Error *",
Content = content,
CloseButtonText = "Ok"
};
// XamlRoot must be set in the case of a ContentDialog running in a Desktop app
ErrorDialog.XamlRoot = this.Content.XamlRoot;
ContentDialogResult result = await ErrorDialog.ShowAsync();
}
Here is how I call the method:
catch (SqlException Sql_Error)
{
Debug.WriteLine($"Hello SQL error GetSQLData_App_View_Results_2020: {Sql_Error.Message}");
DisplayErrorDialog($"GetSQLData_App_View_Results_2020 (SQL Error): {Sql_Error.Message}");
}
catch (Exception Other_Error)
{
Debug.WriteLine($"Hello Other error GetSQLData_App_View_Results_2020: {Other_Error.Message}");
DisplayErrorDialog($"GetSQLData_App_View_Results_2020 (Other Error): {Other_Error.Message}");
}
}
I have many other contentdialogs working successfully within my application. I wanted/needed to test this error contentdialog so I explicitly threw an exception this way:
throw new Exception("SQL test exception #1");
The throw does work and the catch clause 'catches' the exception. However I am getting this error in the contentdialog:
Exception thrown at 0x00007FFFD5FE3FA9 (KernelBase.dll) in MetricReporting.exe: WinRT originate error - 0x80070057 : 'This element is already associated with a XamlRoot, it cannot be associated with a different one until it is removed from the previous XamlRoot.'.
I cannot figure this out. I am a beginner C# and WinUI 3 developer. Thank you for your help and guidance.
Thank you Andrew for your observation.
Here is more of my code:
public pageResults()
{
this.InitializeComponent();
Debug.WriteLine($"### --- InitializeComponents() Completed --- ###");
FetchSQLData_Ref_Metric();
FetchSQLData_Ref_Metric_MarketID();
FetchSQLData_StateCodes();
FetchSQLData_MetricYear();
FetchSQLData_Results();
Display_Results();
}
I am doing a lot of sql fetching of data into datatables and then I display the datatable in a datagrid on the page, which is part of a navigationview.
I inserted the 'throw' inside of a method that is inside of 'FetchSQLData_Results()'
private void FetchSQLData_Results()
{
string metricYear = string.Empty;
// Pre-load all views into their own separate datatables. Each views represents a metric year.
// At this time there are views for 2020, 2021, and 2022
foreach (DataRow row in dtMetricYear.Rows)
{
metricYear = row["metricyear"].ToString();
GetSQLData_App_View_Results_(metricYear);
}
}
private void GetSQLData_App_View_Results_(string metricYear)
{
// Load datatables with existing views
switch (metricYear)
{
case "2020":
GetSQLData_App_View_Results_2020();
break;
case "2021":
GetSQLData_App_View_Results_2021();
break;
case "2022":
GetSQLData_App_View_Results_2022();
break;
case "2023":
break;
case "2024":
break;
default:
break;
}
}
The throw is here:
public void GetSQLData_App_View_Results_2020()
{
try
{
using (SqlConnection con = new SqlConnection("Data Source = xxxx; Initial Catalog = xxxx; Integrated Security = True; Connect Timeout = 15; Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite; MultiSubnetFailover = False"))
{
**//throw new Exception("SQL test exception #1");**
//Prepare sql
//string sql = "select * from dbo.app_emb_prd_lvl_results_2020 order by metric_id";
string sql = "select * from app_emb_prd_lvl_results_2020 v join dbo.Ref_Metric r on v.metric_id = r.Metric_ID order by v.metric_id";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = System.Data.CommandType.Text;
//Open the connection
con.Open();
// Create adapter and fill the datatable with returned data from sql command
using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
{
dtResults2020.Clear();
adap.Fill(dtResults2020);
try
{.. intentionally left blank ..}
catch (Exception List_Error)
{
Debug.WriteLine($"List error GetSQLData_App_View_Results_2020: {List_Error.Message}");
}
Debug.WriteLine($"GetSQLData_App_View_Results_2020 Completed");
} // end using sql adapter
} // end using sql connection
}
catch (SqlException Sql_Error)
{
Debug.WriteLine($"Hello SQL error GetSQLData_App_View_Results_2020: {Sql_Error.Message}");
DisplayErrorDialog($"GetSQLData_App_View_Results_2020 (SQL Error): {Sql_Error.Message}");
}
catch (Exception Other_Error)
{
Debug.WriteLine($"Hello Other error GetSQLData_App_View_Results_2020: {Other_Error.Message}");
DisplayErrorDialog($"GetSQLData_App_View_Results_2020 (Other Error): {Other_Error.Message}");
}
}
As I mentioned in the comments, XamlRoad might not be ready. Try fetching your data in Loaded instead of the constructor.
public pageResults()
{
this.InitializeComponent();
this.Loaded += pageResults_Loaded;
Debug.WriteLine($"### --- InitializeComponents() Completed --- ###");
}
private void pageResults_Loaded(object sender, RoutedEventArgs e)
{
FetchSQLData_Ref_Metric();
FetchSQLData_Ref_Metric_MarketID();
FetchSQLData_StateCodes();
FetchSQLData_MetricYear();
FetchSQLData_Results();
Display_Results();
}

How to improve sqlite write performance in C#

I'm using sqlite to save log and meet write performance issue.
string log = "INSERT INTO Log VALUES ('2019-12-12 13:43:06','Error','Client','This is log message')"
public int WriteLog(string log)
{
return ExecuteNoQuery(log);
}
public int ExecuteNoQuery(string command)
{
int nResult = -1;
try
{
using (SQLiteConnection dbConnection = new SQLiteConnection(ConnectString))
{
dbConnection.Open();
using (SQLiteCommand dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText = command;
nResult = dbCommand.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
// Output error message
}
return nResult;
}
Search in google, transaction could improve the write performance significantly, but unfortunately I don't know when a log message will come, I could not combine the log message. Is there any other way to improve my log write performance?
I tried to add a timer to my code and commit transaction automatically. But I don't think it's a good way to speed up log write performance.
public class DatabaseManager : IDisposable
{
private static SQLiteTransaction transaction = null;
private SQLiteConnection dbConnection = null;
private static Timer transactionTimer;
private long checkInterval = 500;
private DatabaseManager(string connectionString)
{
dbConnection = new SQLiteConnection(connectionString);
dbConnection.Open();
StartTransactionTimer();
}
public void Dispose()
{
if(transaction != null)
{
transaction.Commit();
transaction = null;
}
dbConnection.Close();
dbConnection = null;
}
private void StartTransactionTimer()
{
transactionTimer = new Timer();
transactionTimer.Interval = checkInterval;
transactionTimer.Elapsed += TransactionTimer_Elapsed;
transactionTimer.AutoReset = false;
transactionTimer.Start();
}
private void TransactionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
StartTransation();
transactionTimer.Enabled = true;
}
public void StartTransation()
{
try
{
if (dbConnection == null || dbConnection.State == ConnectionState.Closed)
{
return;
}
if (transaction != null)
{
transaction.Commit();
transaction = null;
}
transaction = dbConnection.BeginTransaction();
}
catch(Exception e)
{
LogError("Error occurs during commit transaction, error message: " + e.Message);
}
}
public int ExecuteNoQuery(string command)
{
int nResult = -1;
try
{
using (SQLiteCommand dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText = command;
nResult = dbCommand.ExecuteNonQuery();
}
}
catch (Exception e)
{
LogError("Error occurs during execute sql no result query, error message: ", e.Message);
}
return nResult;
}
}
This started out as a comment, but it's evolving to an answer.
Get rid of the GC.Collect(); code line.
That's not your job to handle garbage collection - and you're probably degrading performance by using it.
No need to close the connection, you're disposing it in the next line anyway.
Why are you locking? Insert statements are usually thread safe - and this one doesn't seem to be an exception of that rule.
You are swallowing exceptions. That's a terrible habit.
Since you're only ever insert a single record, you don't need to return an int - you can simply return a bool (true for success, false for failure)
Why you don't use the entity framework to do the communications with the database?
For me is the easiest way. It's a Microsoft library so you can sure that the performance is very good.
I made some work with entity framework and sqlite db's and everything works very well.
Here an example of use:
var context = new MySqliteDatabase(new SQLiteConnection(#"DataSource=D:\\Mydb.db;cache=shared"));
var author = new Author {
FirstName = "William",
LastName = "Shakespeare",
Books = new List<Book>
{
new Book { Title = "Hamlet"},
new Book { Title = "Othello" },
new Book { Title = "MacBeth" }
}
};
context.Add(author);
context.SaveChanges();
The type of MySqliteDatabase can be created automatically using database first approach or with Code First approach. You have a lot of information and examples on the internet.
Here the link to the official documentation:
https://learn.microsoft.com/en-us/ef/ef6/

How to retry connecting to database after internet connection has been switched off after some time

i have an notification application built in c# for notifying any change in database . the application runs in the background . But the problem is after the start of the application if the internet is switched off the application throws an SQLException . i have used try catch to handle the exception . but i want my application to try connecting the database and when the connection is established it returns to the main code .
try
{
using (SqlConnection connection =
new SqlConnection(GetConnectionString()))
{
//i want to return here when the connection is reestablished
using (SqlCommand command =
new SqlCommand(GetListenerSQL(), connection))
{
connection.Open();
// Make sure we don't time out before the
// notification request times out.
command.CommandTimeout = NotificationTimeout;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
messageText = System.Text.ASCIIEncoding.ASCII.GetString((byte[])reader.GetValue(13)).ToString();
// Empty queue of messages.
// Application logic could parse
// the queue data and
// change its notification logic.
}
object[] args = { this, EventArgs.Empty };
EventHandler notify =
new EventHandler(OnNotificationComplete);
// Notify the UI thread that a notification
// has occurred.
this.BeginInvoke(notify, args);
}
}
}
catch(SqlException e)
{
}
is it possible to do it without goto statement . i would prefer avoiding the goto statement .
I would move the retry logic out of the query method. I've seen a good retry library somewhere, but I can't find it just now.
public void StartListener()
{
var message = GetMessage();
//process message in some way
object[] args = { this, EventArgs.Empty };
EventHandler notify = OnNotificationComplete;
this.BeginInvoke(notify, args);
}
private const int TimeoutStep = 2000;
private const int MaxTimeout = 10000;
private string GetMessage(int timeout = 0)
{
//prevent loop of endless retries
if (timeout >= MaxTimeout)
{
//optional: define your own Exception class
throw new MaxTimeoutException();
}
try
{
Thread.Sleep(timeout);
return GetMessageFromDatabase();
}
catch (SqlException ex)
{
//log ex in debug mode at least
return GetMessage(timeout + TimeoutStep);
}
}
private string GetMessageFromDatabase()
{
string message = null;
using (var connection = new SqlConnection(GetConnectionString()))
{
using (var command = new SqlCommand(GetListenerSQL(), connection))
{
connection.Open();
command.CommandTimeout = NotificationTimeout;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
message = System.Text.ASCIIEncoding.ASCII.GetString((byte[])reader.GetValue(13));
}
}
}
}
return message;
}
from the suggestion of #Archer i got the solution . in the catch block i call the method again which uses this connection after some suitable time . Something like
public void StartListener()
{
try
{
using (SqlConnection connection =
new SqlConnection(GetConnectionString()))
{
//i want to return here when the connection is reestablished
using (SqlCommand command =
new SqlCommand(GetListenerSQL(), connection))
{
connection.Open();
// Make sure we don't time out before the
// notification request times out.
command.CommandTimeout = NotificationTimeout;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
messageText = System.Text.ASCIIEncoding.ASCII.GetString((byte[])reader.GetValue(13)).ToString();
// Empty queue of messages.
// Application logic could parse
// the queue data and
// change its notification logic.
}
object[] args = { this, EventArgs.Empty };
EventHandler notify =
new EventHandler(OnNotificationComplete);
// Notify the UI thread that a notification
// has occurred.
this.BeginInvoke(notify, args);
}
}
}
catch(SqlException e)
{
Thread.Sleep(2000);
StartListener();
}
}
If its an failure you should call an timer and the timer should call the backgroundworker.
Write the Functionality to check the connection in the backgroundworkers. if it goes for true you should stop the timer. And call the usual process

MySql executing a MySqlScript even when the connection has been closed

Is it possible for mysql to execute a script even when the connection has been closed?
I am using mysql community server , through a .NET connector API.
Was using c# to test out the API.
I have the following static class
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public static class DataBase
{
static string connStr = "server=localhost;user=root;port=3306;password=*******;";
static MySqlConnection conn;
public static bool Connect()
{
conn = new MySqlConnection(connStr);
try
{
conn.Open();
}
catch (Exception Ex)
{
ErrorHandler(Ex);
return false;
}
return true;
}
public static int ExecuteScript(string scripttext) // returns the number of statements executed
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = scripttext;
MySqlScript script;
int count= 0;
try
{
script = new MySqlScript(conn, cmd.CommandText);
script.Error += new MySqlScriptErrorEventHandler(script_Error);
script.ScriptCompleted += new EventHandler(script_ScriptCompleted);
script.StatementExecuted += new MySqlStatementExecutedEventHandler(script_StatementExecuted);
count = script.Execute();
}
catch (Exception Ex)
{
count = -1;
ErrorHandler(Ex);
}
return count;
}
# region EventHandlers
static void script_StatementExecuted(object sender, MySqlScriptEventArgs args)
{
string Message = "script_StatementExecuted";
}
static void script_ScriptCompleted(object sender, EventArgs e)
{
string Message = "script_ScriptCompleted!";
}
static void script_Error(Object sender, MySqlScriptErrorEventArgs args)
{
string Message = "script_Error: " + args.Exception.ToString();
}
# endregion
public static bool Disconnect()
{
try
{
conn.Close();
}
catch (Exception Ex)
{
ErrorHandler(Ex);
return false;
}
return true;
}
public static void ErrorHandler(Exception Ex)
{
Console.WriteLine(Ex.Source);
Console.WriteLine(Ex.Message);
Console.WriteLine(Ex.ToString());
}
}
and I am using the following code to test out this class
using System;
using System.Data;
namespace Sample
{
public class Sample
{
public static void Main()
{
if (DataBase.Connect() == true)
Console.WriteLine("Connected");
if (DataBase.Disconnect() == true)
Console.WriteLine("Disconnected");
int count = DataBase.ExecuteScript("drop database sample");
if (count != -1)
{
Console.WriteLine(" Sample Script Executed");
Console.WriteLine(count);
}
Console.ReadKey();
}
}
}
I noticed that even though I have closed my MySql connection using Disconnect() - which i have defined, mysql continues to execute the command i give next and no error is generated.
I feel like I am doing something wrong, as an error should be generated when i try to execute a script on a closed connection.
Is it a problem in my code/logic or some flaw in mysql connector?
I did check through the mysql workbench whether the command was executed properly and it was.
This is a decompile of MySqlScript.Execute code....
public unsafe int Execute()
{
......
flag = 0;
if (this.connection != null)
{
goto Label_0015;
}
throw new InvalidOperationException(Resources.ConnectionNotSet);
Label_0015:
if (this.query == null)
{
goto Label_002A;
}
if (this.query.Length != null)
{
goto Label_002C;
}
Label_002A:
return 0;
Label_002C:
if (this.connection.State == 1)
{
goto Label_0047;
}
flag = 1;
this.connection.Open();
....
As you can see, when you build the MySqlScript the connection passed is saved in an internal variable and before executing the script, if the internal connection variable is closed, the code opens it. Not checked but I suppose that it also closes the connection before exiting (notice that flag=1 before opening)
A part from this I suggest to change your code to avoid keeping a global MySqlConnection object. You gain nothing and risk to incur in very difficult bugs to track.
static string connStr = "server=localhost;user=root;port=3306;password=*******;";
public static MySqlConnection Connect()
{
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
return conn;
}
This approach allows to write code that use the Using Statement
public static int ExecuteScript(string scripttext) // returns the number of statements executed
{
using(MySqlConnection conn = Database.Connect())
using(MySqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = scripttext;
....
}
}
The Using statement will close and dispose the connection and the command freeing valuable resources and also in case of exception you will be sure to have the connection closed and disposed

C# How do I read data from an SQL Server database as soon as it is inserted?

I have a C# program that works with an SQL Server database. The setup is supposed to work over a LAN.
Now, if I insert a value into the server using a program from Computer A, how would I automatically read that data from the program in Computer B?
Basically, if I, for example, edit a text field and saved the contents to the database, the other program should update its own text field to match that of the host's.
My idea was to periodically check the database every... 2 seconds or so via a timer. Would there be performance problems with that approach? Most of the time I only need to read 1 row at a time, and the contents of the row are simple numbers and strings, nothing too big.
Are there any other approaches to this?
Thanks in advanced.
P.S. Running SQL Server 2005 Express and Visual Studio 2005, if that means anything.
You could periodically check the database from computer b. Add an lastupdated datetime field to the rows and only pull across the rows that have been updated / inserted since the last time the application ran on computer b.
This is much more of a decoupled solution in my opinion
I think you could have a Table where you stored the last update time.
That way you could have a cheap pooling query (just cheching if there where new updates, and only retrieve actual data from database if it was indeed changed).
As already suggested in the comments your best bet is probably to use SqlDependancy on a given SqlCommand to wait on any changes.
You could have computer A send a message (over socket, remoting, etc.) to computer B as soon as an insert is done into the table.
If you're worried about performance, maybe you could only check if the data has been modified when you need it (to modify it, or to display it).
However, if you need to monitor changes, then I would suggest that computer A sends a message to B whenever a change is made...
In addition to all the suggestions, you could also build a basic table watcher (Note this is probably buggy, I wrote it in about 15 min, but should give you the idea). I tested inserts and deletes, obviously it could be refined to detect updates depending on how fancy you want to get. You could create custom event args etc. Also I'm just throwing System.Exceptions because its proof of concept. /end disclaimer
Table Watcher Class:
public class TableWatcher
{
private string mDatabaseConnection;
private string mTableName;
private bool mInitialized = false;
private double mWatchInterval = 10;
private System.Timers.Timer mTableTimer;
private int mInitialRowCount;
private int mCurrentRowCount;
private bool mIsWatching = false;
public event EventHandler RowsAdded;
public event EventHandler RowsDeleted;
protected virtual void OnRowsAdded(EventArgs e)
{
if (RowsAdded != null)
RowsAdded(this, e);
}
protected virtual void OnRowsDeleted(EventArgs e)
{
if (RowsDeleted != null)
RowsDeleted(this, e);
}
public int InitialRowCount
{
get { return mInitialRowCount; }
}
public int CurrentRowCount
{
get { return mCurrentRowCount; }
}
public TableWatcher(string databaseConnection, string tableToWatch)
{
mDatabaseConnection = databaseConnection;
mTableName = tableToWatch;
}
public void Initialize()
{
mInitialized = true;
mInitialRowCount = GetCurrentRows();
mCurrentRowCount = mInitialRowCount;
}
public void StartWatching(double interval)
{
if (mInitialized == false)
{
throw new Exception("Table Watcher has not been initialized. Call Initialize() first.");
}
if (mIsWatching == true)
{
throw new Exception("Table Watcher is already watching. Call Stop Watching to terminate.");
}
if (interval == 0)
{
throw new Exception("Interval is invalid. Please specify a value greater than 0.");
}
else
{
mIsWatching = true;
mWatchInterval = interval;
mTableTimer = new System.Timers.Timer(mWatchInterval);
mTableTimer.Enabled = true;
mTableTimer.Elapsed += new System.Timers.ElapsedEventHandler(mTableTimer_Elapsed);
}
}
void mTableTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
mTableTimer.Enabled = false;
int rowCount = GetCurrentRows();
if (rowCount > CurrentRowCount)
{
OnRowsAdded(new EventArgs());
}
else if (rowCount < CurrentRowCount)
{
OnRowsDeleted(new EventArgs());
}
mCurrentRowCount = rowCount;
mTableTimer.Enabled = true;
}
private int GetCurrentRows()
{
int currentRows = 0;
using (SqlConnection conn = new SqlConnection(mDatabaseConnection))
{
conn.Open();
string query = String.Format("Select Count(*) from {0}", mTableName);
using (SqlCommand cmd = new SqlCommand(query, conn))
{
object rows = cmd.ExecuteScalar();
if (rows != null)
{
currentRows = Convert.ToInt32(rows);
}
}
}
return currentRows;
}
public void StopWatching()
{
mTableTimer.Enabled = false;
mInitialized = false;
mIsWatching = false;
}
}
Usage:
string dbConn = "Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;";
string tableName = "TestTable";
TableWatcher t = new TableWatcher(dbConn, tableName);
t.RowsAdded += new EventHandler(t_RowsAdded);
t.RowsDeleted += new EventHandler(t_RowsDeleted);
t.Initialize();
t.StartWatching(100);
Running from a console app:

Categories