Click here to Skip to main content
15,886,806 members
Articles / Web Development / ASP.NET

ASP.NET User Controls - Notify One Control Of Changes In Other Control

Rate me:
Please Sign up or sign in to vote.
4.39/5 (23 votes)
12 Mar 20026 min read 195.4K   2.6K   87  
A tutorial on how to use delegate model to notify one ASP.NET user control of changes in the other user control.
// Disclaimer and Copyright Information
// WebLogDBManager.cs : 
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.0
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomarix@pardesiservices.com
///////////////////////////////////////////////////////////////////////////////
//

using System;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace ASPNet_App
{
	/// <summary>
	/// 
	/// </summary>
	public class WebLogDBManager
	{
		/// <summary>
		/// 
		/// </summary>
		private static string m_strConnString = "Data Source=localhost; Initial Catalog=WebLog; user id=sa;pwd=;";

		/// <summary>
		/// 
		/// </summary>
		public WebLogDBManager()
		{
			// 
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="doc"></param>
		/// <returns></returns>
		public bool AddInfoToDatabase(XmlDocument doc)
		{
			bool bRetVal = true;
			// Open the database connection.
			// Iterate through each InfoNode in XML document and add
			// it to database.
			XmlNodeList elemList;
			try
			{
				XmlElement docElement = doc.DocumentElement;
				if (docElement != null)
				{
					// Get the list of all InfoNode xml noded fromthe document.
					elemList = docElement.SelectNodes("//brow_info");
					foreach (XmlNode infoNode in elemList)
					{
						this.AddInfoNodeToDB(infoNode);
					}
				}
			}
			catch
			{
				bRetVal = false;
			}

			return bRetVal;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="infoNode"></param>
		/// <returns></returns>
		private bool AddInfoNodeToDB(XmlNode infoNode)
		{
			SqlConnection  dbConn = null;
			StringBuilder strInsertQuery = new StringBuilder();
			try
			{
				dbConn = new SqlConnection (m_strConnString);
				// open the database connection
				dbConn.Open();

				strInsertQuery.Append("insert into browsercapslog (");
				strInsertQuery.Append("clienturl, browsername, browserversion, browsermajorversion" +
					",browserminorversion, supportcookies, supportactivex" +
					",supportjavascript, supportjavaapplets, supportvbscript" +
					",supportbackgroundsounds, supportframes, supporttables" +
					",supportcdf, isaol, iscrawler, isbetabrowser, w3cdomversion" +
					",msdomversion, clrversion, ecmascriptversion, clientplatform)" +
					" values(");
				
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "clienturl") + "'");
				strInsertQuery.Append(",");
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "browsername") + "'");
				strInsertQuery.Append(",");
				strInsertQuery.Append(Decimal.Parse(GetXmlNodeValue(infoNode, "browserversion")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "browsermajorversion")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Decimal.Parse(GetXmlNodeValue(infoNode, "browserminorversion")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportcookies")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportactivex")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportjavascript")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportjavaapplets")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportvbscript")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportbackgroundsounds")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportframes")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supporttables")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "supportcdf")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "isaol")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "iscrawler")));
				strInsertQuery.Append(",");
				strInsertQuery.Append(Int32.Parse(GetXmlNodeValue(infoNode, "isbetabrowser")));
				strInsertQuery.Append(",");
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "w3cdomversion") + "'");
				strInsertQuery.Append(",");
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "msdomversion") + "'");
				strInsertQuery.Append(",");
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "clrversion") + "'");
				strInsertQuery.Append(",");
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "ecmascriptversion") + "'");
				strInsertQuery.Append(",");
				strInsertQuery.Append("'" + GetXmlNodeValue(infoNode, "clientplatform") + "'");

				strInsertQuery.Append(")");

				// Prepare the insert command.
				SqlCommand insertCmd = new SqlCommand(strInsertQuery.ToString(), dbConn);
				int nRecsAffected = insertCmd.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				Trace.WriteLine(ex.Message);
				return false;
			}
			catch (InvalidOperationException ex)
			{
				Trace.Write(ex.Message);
				return false;
			}
			catch
			{
				Trace.Write("Failed during opeing database connection process");
				return false;
			}
			finally
			{
				if (dbConn != null && dbConn.State == ConnectionState.Open)
				{
					dbConn.Close();
				}
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strName"></param>
		/// <returns></returns>
		public DataSet GetBrowsersInfo(string strName)
		{
			string strQuery = "SELECT COUNT(*) AS RecCount FROM BrowserCapsLog WHERE browsername='";
			strQuery += strName;
			strQuery += "'";
			DataSet dtSet = null;
			try
			{
				dtSet = this.GetLogRecords(strQuery);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return null;
			}

			return dtSet;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public long GetNumRecords()
		{
			long lNumRecords = -1;
			DataSet dtSet = null;
			string strQuery = "SELECT COUNT(*) AS RecCount FROM BrowserCapsLog";

			try
			{
				dtSet = this.GetLogRecords(strQuery);
				string strVal = dtSet.Tables[0].Rows[0]["RecCount"].ToString();
				lNumRecords = Int32.Parse(strVal);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return -1;
			}

			return lNumRecords;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strTag"></param>
		/// <returns></returns>
		private string GetXmlNodeValue(XmlNode infoNode, string strTag)
		{
			string strRet = "";
			try
			{
				XmlNode valNode = infoNode.SelectSingleNode("//" + strTag);
				if (valNode != null)
				{
					strRet = valNode.InnerText;
				}
			}
			catch (XmlException ex)
			{
				Trace.WriteLine(ex.Message);
			}
			catch
			{
				Trace.WriteLine("Exception while accessing information from node");
			}

			return strRet;
		}

		private string GetYesNoString(string strVal)
		{
			if (strVal.Length == 0 || strVal.CompareTo("0") == 0)
			{
				return "No";
			}
			else if (strVal.CompareTo("1") == 0)
			{
				return "Yes";
			}

			return "No";
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strQuery"></param>
		/// <returns></returns>
		private DataSet GetLogRecords (string strQuery)
		{
			// If the query string is empty, simply return a null value.
			if (strQuery.Length == 0)
			{
				return null;
			}

			DataSet retDS = null;
			
			SqlConnection dbConn;
			//"SELECT * FROM table WHERE theDateField BETWEEN '" & firstDate & "' AND '" & lastDate & "'"
			try
			{
				dbConn = new SqlConnection(m_strConnString);

				// Open the connection.
				dbConn.Open();
				SqlDataAdapter dbAdapter = new SqlDataAdapter(strQuery, dbConn);
				retDS = new DataSet();

				// Fill the dataset with records from the database.
				dbAdapter.Fill(retDS);
			}
			catch (SqlException ex)
			{
				Trace.WriteLine(ex.Message);
			}

			return retDS;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

Comments and Discussions