Category Archives: C#

Reading a CSV File with an OleDbDataAdapter and Jet

Reading a comma delimited is quite simple with an OleDbDataAdapter and Jet 4.0: private static DataTable ReadCsvFileViaJetOleDb(string file) { string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + Path.GetDirectoryName(file) + "\\\";Extended Properties=\"text;HDR=Yes;FMT=Delimited\""; string query = "SELECT * FROM " + file; DataTable dt … Continue reading

Posted in C# | Leave a comment

Sys.WebForms.PageRequestManagerServerErrorException in FireFox with MSFT Ajax

If you encounter this error while navigating away from an AJAX-enabled page with a non-IE browser, you can silence it with the following JavaScript: if (!document.all) //non-ie { window.onbeforeunload = function() { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(ignoreOnEndRequest); } } function ignoreOnEndRequest(sender, e) { err … Continue reading

Posted in ASP.Net, C#, FireFox, JavaScript | 1 Comment

ASP.Net WebServices: Enabling or Disabling POST, GET, and Documentation Requests

It is relatively easy to control access to the various HTTP request types (protocols) for your ASP.Net WebService. Add something like this to the system.web section of your web.config: <webServices> <protocols> <add name="HttpSoap"/> <add name="HttpGet"/> <!– Because this web service … Continue reading

Posted in C#, Web Services, WinForms | Leave a comment

Regular Expression (RegEx) to Find Whole Words in a String

Ever want to match and replace whole words with in a string?  Regular expressions (System.Text.RegularExpressions) makes it a one line operation: Regex.Replace(inputText, @"\b" + wordToReplace + @"\b", replacementText, RegexOptions.IgnoreCase); This pattern uses “word boundries” as your delimiters for matching text.

Posted in C#, Regular Expressions | Leave a comment

WinForm Exception Catching

Here’s a general way to catch most outside exceptions in your WinForm code.  Add this to your program wrapper: Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Of course, it is always better to handle specific cases with specific types … Continue reading

Posted in C#, WinForms | Leave a comment

How much time does a method take to run?

I frequently run into the situation where I quickly want to learn how fast one method or section of code runs in my unit testing.  Here’s a quick and dirty way to get it done: long startTime = DateTime.Now.Ticks; ExecuteMyMethod(); … Continue reading

Posted in C# | Leave a comment

WCF: System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL

If you are getting the “Your process does not have access rights to this namespace” error after switching your WCF service to run under a different account, try the following: 1) Install the Windows support tools from your Windows 2003 … Continue reading

Posted in C#, WCF | Leave a comment

Add your WebControls to your Web.Config to Simplify References

Ever get sick of repeating the same user control registrations at the top of your user controls, pages, and master pages within your web application?  If so, you might want to try the following to save your self from all … Continue reading

Posted in C# | Leave a comment

Dynamically Instantiate Objects from Another Assembly within the Current AppDomain

Reflection is one of the coolest features in .Net.  But to support instantiation of objects from different assemblies within your application domain requires a little finagling: // try this assembly first; full className e.g. MyNameSpace.Pattern.MyClass Type typeToCreate = Type.GetType(className); if … Continue reading

Posted in C# | Leave a comment

Find the Previous Method Called

To determine what the previous method called was prior to your current location in the code, try the following: string previousMethod = new System.Diagnostics.StackFrame(1, false).GetMethod().Name; This is a great quick way to determine the previous method for logging purposes in … Continue reading

Posted in C# | Leave a comment