Articles tagged under c#

Title Case

ToTitleCase - Making text Title Case Whilst returning some data from LDAP, I found that it was returning in Upper Case, I needed it to be in Title case format. I found this method of converting to title case Title Case converts every first letter of a word in a sentence to upper

Danny Mehmed - 3,033 views

Set Input Focus on a Control Programatically Added to an ASP.Net Page

Set Input Focus on a Control Programatically Added to an ASP.Net Page This little problem had the potential to drive me nuts for hours, I was fortunate to stumble over a blog with 100s of comments re problems of this nature and how to solve it. Most of the discussion was about using JavaScr

Paul Marshall - 3,578 views

Getting the Virtual Path of a Request in c#

Getting the Virtual Path of a Request in c# This simple bit of code will get you the Virtual Path of your current request. public static string GetVirtualPath(string url) {     if (HttpContext.Current.Request.Application

Paul Hayman - 18,462 views

Fixing Form Action on UrlRewrite (UrlRewriter)

Fixing Form Action on UrlRewrite IF you're rewriting URL's, you've probably come across the same problem I had. When posting back on the page, the real URL is used.. this is a real pain if you're writing a page which uses the RawUrl to serve up relevant content. I had a good loo

Paul Hayman - 5,361 views

Returning a value from a PageMethod

Returning a value from a PageMethod using MS AJAX NOTE: This is designed for v1.0.61025.0 of AJAXExtensionsToolbox.dll If you've ever returned data from a web service using MS AJAX this technique will look familar. Call the method from javascript by append PageMethods. to the fro

Paul Hayman - 37,805 views

Sorting an XML document in C# using XSL

Sorting an XML document in C# using XSL I needed to sort some XML in C# before itterating through the document. In the end I used the XslCompiledTransform to apply an XSL stylesheet to the XML document. Works very quickly. For reference, this is how I did it. The code The follo

Paul Hayman - 19,617 views

Using a .Net 2.0 anonymous delegate when generating a thumbnail

Using a .Net 2.0 anonymous delegate when generating a thumbnail Rather than using a mehod which does nothing except return false. Use a .Net 2.0 anonymous delegate for the GetThumbnailImageAbort parameter. delegate() { return false; } Example System

Paul Hayman - 3,044 views

Using Enum.Parse()

Using Enum.Parse() Have you ever written any code along the lines of... myEnum GetEnumValue(string EnumString) {     myEnum enumValue;     switch (EnumString)     { &n

Dave Howard - 25,265 views

Convert to and from Hex

Convert to and from Hex Convert from an int to Hex String result = String.Format("{0:x2}", 255) Convert from Hex to an int int result = int.Parse("FF", System.Globalization.NumberStyles.HexNumber);

Paul Hayman - 3,014 views

"This row already belongs to another table" Error

"This row already belongs to another table" Error What an annoying error!! I couldn't believe that I couldn't just add a Row from one DataTable to another, while looping around a DataTable's Rows. It turned out to be slightly more complicated than the code I had written, I've si

Paul Marshall - 138,153 views

Why we use C# for .NET development

Why we use C# for .NET development In the company where we work, we have a policy of using Java for all major developments. The core reason behind this being that we have a long term aspiration to being vendor and platform independent. Regards the .NET framework the policy is to

Dave Howard - 37,601 views

Encoding string to Base64 / UTF8

Encoding string to Base64 / UTF8 Sometimes you need to convert strings to Base64, for example in security components. The function below does this: public string stringToBase64(string s) {     try     

Dave Howard - 20,586 views

Authenticated HTTPRequests (Using Credentials)

Authenticated HTTPRequests (Using Credentials) Recently I had to make a SOAP call to a Cisco CallManager (an IP PBX). This required that the request used basic authentication. I couldn't add a web reference as the cisco interface doesn't support GET requests, so the Visual Studio discovery

Dave Howard - 81,400 views

Web service calls from javascript using ATLAS (part 3) - Complex properties

Web service calls from javascript using ATLAS (part 3) - Complex properties Part two covered returning objects with multiple properties from a webservice and accessing those properties from javascript. This article demonstrates that even 'complex' properties are supported by the ATLAS

Dave Howard - 5,276 views

Web service calls from javascript using ATLAS (part 2) - Complex return types

Web service calls from javascript using ATLAS (part 2) - Complex return types Following on from part one which showed how to make web service calls from javscript, this article will now show the flexibility of the ATLAS framework by calling a web service that returns an object from jav

Dave Howard - 6,945 views

Web service calls from javascript using ATLAS (part 1)

Web service calls from javascript using ATLAS (part 1) ATLAS provides a fantastic mechanism for making web service calls from javascript code. Take the simple web service below... public class myWebService : System.Web.Services.WebServic

Dave Howard - 5,864 views

Auto-complete using ATLAS

Auto-complete using ATLAS Setting up an auto-complete input box in ATLAS is pretty simple, here is a quick guide to illustrate this... Assuming you already have the ATLAS framework referenced in your project then select an <asp:TextBox> to apply auto-complete to. See Mark

Dave Howard - 8,511 views

Creating a Tag Cloud in C#

Creating a Tag Cloud in C# I wanted to add a TagCloud to GeekZilla and another bespoke development project. The first thing I did was look around the web for some examples (or free components). To my supprise none jumped out at me. So, I decided to write my own. At first I found myself

Paul Hayman - 73,943 views

Custom Templated SiteMap Navigator Control

Custom Templated SiteMap Navigator Control After discovering the power of the SiteMap, especially when linked to Authentication, it wasn't long before binding the TreeView to the map was not enough. I needed to have total control over what was displayed to the user for each item, so I

Paul Hayman - 6,152 views

Redirect a page using a Header

Redirect a page using a Header Any keen surfer will be familiar with Affiliate Marketing, its a huge industry with Google's Adwords driving "Pay Per Click" campaigns. Google provide the Adwords application and Google Analytics to track the effectiveness of marketing campaigns from click thr

Paul Marshall - 15,917 views

The null coalescing operator: ??

The null coalescing operator: ?? This is a new feature of c# 2.0. The null coalescing operator is a short cut for checking if a value is null and if so returning the value of the second operand. Kind of like an IIF. The syntax is as follows: string newValue = someVa

Paul Hayman - 4,410 views

Restricting the number of rows in a dataview when binding to a repeater

Restricting the number of rows in a dataview when binding to a repeater The problem I needed to restrict the number of items bound to an asp:repeater. Basically I never wanted more than 10 items to be displayed. Options The right thing to do would be to restrict the number of

Paul Hayman - 13,224 views

Comparing ATLAS and Dojo (part 1)

Comparing ATLAS and Dojo (part 1) This is a first article in a series comparing some of the functionality of ATLAS with another popular AJAX framework called Dojo. Both frameworks have some powerfull components and there is a high degree of overlap. This first article is a straight com

Mark Page - 12,752 views

Using Regex.Replace

Using Regex.Replace Say you want to get the username from a fully qualified username such as MyDomain\AUser Most developers would turn to SubString and LastIndexOf functions .. for example string userName = @"MyDomain\AUser"; string result = userNam

Paul Hayman - 11,726 views

Programmatically adding LINKs to HTML HEADER in Code Behind

Programmatically adding LINKs to HTML HEADER in Code Behind In this example I am adding a link to the html header pointing to an rss feed. HtmlLink link = new HtmlLink(); link.Attributes.Add("type", "application/rss+xml"); link.Attributes.Add("rel",&

Paul Hayman - 11,311 views

Generating a resized image with maximum image quality

Generating a resized image with maximum image quality Here's some code I use for generating a resized image with no Jpeg compression. public MemoryStream GetResized(Bitmap originalImage, int width, int height) {

Paul Hayman - 3,083 views

Adding ATLAS to an Existing Web Application

Adding ATLAS to an Existing Web Application Recently had to add ATLAS to an existing ASP.NET 2.0 application and found that there is more to it than simply adding a reference to the Microsoft.Web.Atlas.dll. I tried registering the assembly in an <@ Register> tag after adding the

Mark Page - 19,064 views

Integrating Dojo Fisheye and SiteMapDataSource

Integrating Dojo Fisheye and SiteMapDataSource Everybody seems to be getting excited over this AJAX thing. Even Microsoft are (at last... ok they started it with OWA) getting on the bandwagon with the Atlas libraries. Of course just because Atlas exists doesn't mean you cannot use some

Mark Page - 13,717 views

Extending web.sitemap

Extending web.sitemap If you are using ASP.NET you are probably already familiar with the new sitemap components. The sitemapdatasource and web.sitemap elements make it very easy to build and maintain rich site structures. Using the Eval statement and recursing across the siteMap

Mark Page - 10,809 views

Extending Strongly Typed Datasets

Extending Strongly Typed Datasets Strongly Typed Datasets are a rather nice addition to VS2005 (they don't lend themselves well to acronyms however) You still need to create a database and tables and realationships but using STDs saves a writing huge amount of code. If this code

Dave Howard - 6,169 views

Triple DES encryption wrapper

Triple DES encryption wrapper Here is a handy wrapper for Triple DES encryption:

Paul Hayman - 21,181 views

Serializing abstract classes to XML

Serializing abstract classes to XML Digg it: http://digg.com/programming/Serializing_abstract_classes_to_XML_in_c It is often useful to have abstract classes with several derived types to allow use of strongly typed lists and the such. For example you might have a DocumentFragmen

Dave Howard - 21,745 views

Launching an Atlas ModalPopupExtender from JavaScript

Launching an Atlas ModalPopupExtender from JavaScript Looking for an Ajax version of this article?? Looking round the web there are some crazy ways people are trying to launch the ModalPopupExtender from JavaScript, the most common way being setting a timeout which calls the Click meth

Paul Hayman - 54,181 views

Could not write to output file ... The directory name is invalid

Could not write to output file ... The directory name is invalid Recently had a problem deploying an app to a fresh 2003 server. Basically, the application wouldn't start, instead we recieved an error stating that the compiler couldn't write to the "Temporary ASP.NET Files" folder because th

Paul Hayman - 15,956 views

Remembering position of DragOverlayExtender with Profile

Remembering position of DragOverlayExtender with Profile You can get Atlas to store the last known position of your floating panel in the profile. Three things you need to do to get this to work. ProfileScriptService In the page with the DragOverlayExtender you'll need to

Paul Hayman - 5,532 views

Reading a file which is locked by another process

Reading a file which is locked by another process The following code snippet shows how to read a file which is locked by another process.. FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read,&nb

Paul Hayman - 33,092 views

EasyHTTP

EasyHTTP EasyHTTP is a simple class which exposes the HTTP functionality of .NET as a number of simpler methods.

Barrington Haynes - 6,152 views

CLR Stored Procedure for searching files

CLR Stored Procedure for searching files I was recently asked to write a CLR stored procedure which would process a text file and return a row for each line in the file that contained text matching our search criteria. I'd never written a CLR stored proc before so it was an interesting

Paul Hayman - 15,636 views

Writing CLR Stored Procedures in C#

Writing CLR Stored Procedures in C# As SQL Server 2005 rolls out DBA's are going to be forced to learn either C# or Visual Basic or both. Until now these were client side languages and not knowing them had little impact on your job. And if you write code in these languages your going to have

Paul Hayman - 15,842 views

IsGuid() (Regular Expression Guid Match)

IsGuid() (Regular Expression Guid Match) A regular expression for validating a string as being a Guid is.. @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" Example usage Below is a function I try to keep handy which t

Paul Hayman - 82,519 views

Programmatically resolving ~ URL's to the Virtual Root using ResolveURL()

Programmatically resolving ~ URL's to the Virtual Root using ResolveURL() It's common knowledge that a control, when Runat="server" will resolve it's src or href attribute to a virtual root when the URL starts with ~/ For example: <a href="~/Customers/Profile.aspx" Runat=

Paul Hayman - 36,431 views

Updating controls from another thread

Updating form controls from another thread. Form controls, by default, can only be updated by the forms thread. Otherwise an invoke must be used. The example below shows a simple logging function that can be called by threads other than the forms. delegate void LogSa

Dave Howard - 4,961 views

System tray and balloon tips

Balloon Tips from the System Tray You can really easily add system tray function to .NET windows forms by dragging the NotifyIcon object onto your form. You can then use this to display balloon tips from the system tray with the code below: myNotifyIcon.ShowBalloonTip(500,&nbs

Dave Howard - 30,424 views

Assigning an array of strings to a variable

Assigning an array of strings to a variable Declaring a string array with hard coded data is easy. string[] newString = new string[] {"one", "two", "three"} Passing hard coded array into method Likewise, passing an array of hard coded data

Paul Hayman - 4,099 views