Archive for the ‘Programming’ Category

Get Server Computer Name in C#

The situation is I have an existing C# web application (.NET 3.5) hosted on Windows Server 2003 on a company Intranet. I want to get the name of the computer on which the web application is hosted. This can be achieved by:

Environment.MachineName

Environment requires the System namespace.

Encode HyperLink in GridView using Custom Expression Binding

In my C# Web Application, I have a GridView with a TemplatField. Within the TemplateField is a HyperLink control. My goal is to bind this HyperLink control using Custom Expression Binding. I want the NavigateUrl to point to:

Visit.aspx?name=XXX

XXX in the link above is the database column name. My first instinct is to use the Custom Expression Binding method called Eval() to bind this column. An example of the use of this semi-working method below should be included in the <asp:HyperLink> control.

<asp:HyperLink NavigateUrl=’<%# “Visit.aspx?name=” + Eval(“name”) %>’></asp:HyperLink>

I say this semi-works because the evaulted “name” must not contain any special characters like the ampersand (&). Then the next idea is trying to encode the Eval(“name”). This however was not entirely clear. After researching, I found the following solution.

<asp:HyperLink NavigateUrl=’<%# “Visit.aspx?name=” + HttpUtility.UrlEncode(Eval(“name”).ToString()) %>’></asp:HyperLink>

And this is how to properly encode a hyperlink url that is bound using Custom Expression Binding. Though this may not be visible from the browser, the web page source will reveal that the ampersand (&) has been replaced with %26, which is its equivalent.

The last step on the following page my require decoding the encoded url.

Unable to add to the Web site. Unable to add file. Access is denied.

While taking over a C# Web Application that had been migrated to work with Visual Studio 2008 (originally developed and migrated from Visual Studio .NET 2003), I occasionally ran into issues while trying to publish the web application to the production server (or any server for that matter). In the output log, there were multiple error messages, all stating:

Unable to add ‘XXX.aspx’ to the Web site. Unable to add file ‘XXX.aspx’. Access is denied.

Each XXX.aspx would be a different files. What I learned after browsing and searching Google is to make sure the files on the production (or target) server are not marked as Read Only. The solution was to:

  1. Go to the Publish directory
  2. Highlight all files and folders
  3. Right click and select Properties
  4. Make sure the Read-only attribute checkbox is unchecked (white and not gray or checked)

References:
http://forums.asp.net/p/1074678/3431238.aspx#3431238