Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Friday, 4 May 2012

asp.net PasswordRecovery control uses ControlState to persist data

Took me a while to figure out why PasswordRecovery control persisted its "Success" state between postbacks if MailError happened, even when EnableViewState="false"

One of its internal properties is CurrentView which can be UserName or Success. This property is not available for use and it is persisted between postbacks in the ControlState.

My problem was that the state of the control stayed as "Success" after I catched the smtp error in OnSendMailError handler by setting e.Handled = true; .



Friday, 24 February 2012

Fixing Gallio ReSharper Runner file not found exception

Visual Studio 2010. Windows 7 x64. ReSharper 5.1. Gallio 3.3.458.0, installed from 64 bit msi.

When debugging a unit test written with Gallio Framework, there is the following system.IO.FileNotFound exception : {"Could not load file or assembly 'Gallio.ReSharperRunner51, Version=3.3.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"Gallio.ReSharperRunner51, Version=3.3.0.0, Culture=neutral, PublicKeyToken=null"}

You click "Continue" and test debugging continues as normal.

It appears that it looked for the Gallio.ReSharperRunner51.DLL in the following paths

C:/Program Files/Gallio/bin
C:/Program Files/Gallio/bin/Gallio.ReSharperRunner51

and the file is not there.

Quick solution would be to get that dll from the zip "bundle" and copy it to C:/Program Files/Gallio/bin.



Wednesday, 14 December 2011

How to prevent xml encoding of an entity as attribute value


XmlDocument doc = new XmlDocument();
doc.LoadXml("<book><title>War &amp; Peace</title></book>");
//doc.LoadXml("<book Cost=\"456\"><title>War &amp; Peace</title></book>");

XmlElement root = doc.DocumentElement;

XmlAttribute att = doc.CreateAttribute("Cost");
att.InnerXml = "123 &#8364;";
root.Attributes.Append(att);

//will properly set the value of Cost attribute to 123
// <book Cost="123 €"><title>War &amp; Peace</title></book>
// and will overwrite already existing attribute, if any

Wednesday, 28 September 2011

Telerik RadTreeList - how to switch button(s) for an item at runtime

Struggled with this for some time - Wanted to be able to switch Insert and\or Edit buttons on a TreeList control from Telerik at runtime, depending on what its DataItem or NestedLevel is. Here is the solution, using the control's server-side API:


protected void OnMyTreeListItemCreated(object sender, TreeListItemCreatedEventArgs e)
{
//APPROACH #1
if (e.Item.ItemType == TreeListItemType.AlternatingItem || e.Item.ItemType == TreeListItemType.Item)
{
TreeListDataItem item = e.Item as TreeListDataItem;
if (item != null)
{
int level = item.HierarchyIndex.NestedLevel;
//DO THE JOB
LinkButton btnInsert = item["EditCommandColumn"].FindControl("InsertButton_EditCommandColumn") as LinkButton;
LinkButton btnEdit = item["EditCommandColumn"].FindControl("EditButton_EditCommandColumn") as LinkButton;


if (btnInsert != null) { btnInsert.Visible = (level == 0); }
if (btnEdit != null) { btnEdit.Visible = (level > 0); }
}
}



//APPROACH #2
var editableItem = e.Item as TreeListEditableItem;


if (editableItem != null)
{

SomeMyType boo = editableItem.DataItem as SomeMyType;


if (boo!= null && boo.SomeProperty == "SomeValue")
{
//SAME DO THE JOB AS IN #1
}
}

}




}

Monday, 5 September 2011

Calling generic method, determining the type at runtime

//using Linq:
string paramTypeName = "System.Int32";
string someValue = "33";

Type paramType = Type.GetType(paramTypeName);

MethodInfo mymethod = typeof(MyLibrary.MyClass)
    .GetMethods(BindingFlags.Public | BindingFlags.Static)
    .First(m => m.Name == "MyGenericMethod" && m.GetParameters().Count() == 2);
 
object methodResult = mymethod.MakeGenericMethod(paramType).Invoke(null, new object[] { 123, someValue });


inspired  by:
http://www.aaron-powell.com/reflection-and-generics
http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method

Thursday, 4 August 2011

How to copy one folder contents into another in C#

/// <summary>
  /// Recursively copies one folder contents into another folder. Creates destination folder if it doesn't exist
  /// </summary>
  /// <param name="source"></param>
  /// <param name="target"></param>
  public static void CopyFilesTree(DirectoryInfo source, DirectoryInfo target)
  {
   if (!Directory.Exists(target.FullName)) { Directory.CreateDirectory(target.FullName); }
 
   foreach (DirectoryInfo dir in source.GetDirectories()) {
    CopyFilesTree(dir, target.CreateSubdirectory(dir.Name));
   }
 
   foreach (FileInfo file in source.GetFiles()) {
    file.CopyTo(Path.Combine(target.FullName, file.Name));
   }
  }

Friday, 29 July 2011

asp.net CodeBehind vs CodeFile

Applicable to ASP.Net 2.0 onwards

if it is a "web application" project - use CodeBehind
if it is a "website" project - use CodeFile

in regular ascx file:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="My.Great.Web.SomeCtrl" CodeBehind=SomeCtrl.ascx.cs" %>


in regular aspx file:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="My.Great.Web.SomePage" Codebehind="SomePage.aspx.cs" %>


in global.asax:

<%@ Application Language="C#" Inherits="My.Great.Web.Global" CodeBehind="Global.asax.cs" %>
More info here: ASP.NET Web Page Code Model

Friday, 17 June 2011

Configuring managed code that runs via COM interop

Scenario: .Net managed code library (assembly) depends on settings in a .config file. The library is used via COM interop from some unmanaged code application (i.e. MyLegacyGame.exe) written, say, in C++


Question: what should be the name of the .config file ?


Answer: MyLegacyGame.exe.config


There is a problem with that, however, if your native app is in fact part of windows