Wednesday 11 July 2007

Web.config problem: The entry '' has already been added

We are going to production with our ASP.NET 2.0 application and we had a problem with configuration of logger from Micosoft Enterprise libraries. It depends on how the application is configured in IIS server. We had configured access to application from two different addresses and if we accessed from the nested one. It gives the error.

The error is described on the following url:

Enterprise Library Error: The entry 'ExceptionPolicy' has already been added

Wednesday 20 June 2007

ASP.NET and infragistics WebDateChooser component

I needed to implement couple of javascript things into ASP.NET application. I had problem with infragistics component for dates (WebDateChooser). I needed two peaces of this component to have disabled after page load in some cases. On the page should be several radio buttons and some other components which need to be enabled only if the appropriate radio button is selected. But there were following problems with the component:
1) when the component was disabled in codebehind, I could not easily enable it in javascript if needed.
2) when I wanted to disable the component after page loaded, the component was not initialized so I could not work with it.

The solution in both cases was found, maybe it can help somebody who works with the infragistics components which sometimes do not work as you need.

1) component was disabled in codebehind in PageLoad method.

this.CurrentDateChooser.Enabled = false;

In javascript which handles click on radiobutton you need to get the following:

var currentChooser = igdrp_getComboById(currentChooserName);
currentChooser.setEnabled(true);
currentChooser.elemCal.disabled = false;

Let me explain the javascript. Method igdrp_getComboById retrieves the date chooser component by id. On the second row we need to enable the component but unfortunatelly this is not enough. Component is enabled but at least in IE 6.0 the calendar is still not enabled and you can not choose the date. This is solved by the last row. In FF 2.0 it works even without the last row of code. Maybe it is problem of the old IE.

2) The solution is quite easy. It says run the script after the page is loaded and all other initialization scripts are finished. I had to place the code at the end of master page like in the following code:

<script language="javascript">
if (typeof(checkState) != "undefined") checkState();
</script>

And now we have to define the checkState() method. This method will call the same code as the method which handles selection of radio buttons. So I added in the aspx page the following code, which calls the same method.

<script language="javascript">
function checkState() {
OnRadioClick(<%= this.SelectedRadioButton %>,
'<%= this.IntervalDropDownList.ClientID %>',
'<%= this.CurrentDateChooser.ClientID %>',
'<%= this.PreviousDateChooser.ClientID %>')
}
</script>


I prefer the solution number 1 because it is much clearer for me.

Thursday 31 May 2007

Resolving DNS computer name

I needed to resolve computer name from the client IP address. So if you ever need this you can use the code bellow. It is really easy.


public static string ResolveComputerName(string IPaddress)
{
return System.Net.Dns.GetHostEntry(IPaddress).HostName;
}

Tuesday 24 April 2007

Active directory service interface reference

I needed to retrive some values from Ldap and for some properties I was not able to retrieve its value. I used standard classes from namespace System.DirectoryServices and always got System.Runtime.InteropServices.COMException(0x8000500c). After some investigation I found that for access to Ldap I can use ADSI interface. I found that the property I wanted to get was of octet string datatype. Unfortunatelly I did not managed to find how to retrieve valuo of the property though PropertyCollection. I was able to find that the collection contains the property, but I could not read its value.

At first I used something like that:

// works almost for everything, but not for octet strings (array of bytes)
// properties is of type PropertyCollection from System.DirectoryServices
string value = string.Empty;

if (properties.Contains(key))
{
value = properties[key].Value.ToString();
}


Solution of the problem:

string value = string.Empty;

try
{
// if the item do not exists exception is thrown
ActiveDs.IADsPropertyEntry entry = ActiveDs.IADsPropertyEntry)propertyList.Item(key);

// get types
int propertyType = entry.ADsType;
int valueType = (ActiveDs.IADsPropertyValue)((object[])((ActiveDs.IADsPropertyEntry)propertyList.GetPropertyItem(key, propertyType)).Values)[0]).ADsType;

switch (valueType)
{
// for octet string (array of bytes) we have to create string
case (int)ActiveDs.ADSTYPEENUM.ADSTYPE_OCTET_STRING:
value = Sstem.Text.Encoding.ASCII.GetString((byte[])((ActiveDs.IADsPropertyValue)((object[])((ActiveDs.IADsPropertyEntry)propertyList.GetPropertyItem(key, propertyType)).Values)[0]).OctetString);
break;

// for all others - all current properties are of this type
case (int)ActiveDs.ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING:
value = ((ActiveDs.IADsPropertyValue)((object[])((ActiveDs.IADsPropertyEntry)propertyList.GetPropertyItem(key, propertyType)).Values)[0]).CaseIgnoreString;
break;
// in other cases you can add new section
}
catch (System.Runtime.InteropServices.COMException cex)
{
if (cex.ErrorCode == -2147463155)
Trace.Write(cex);
else throw;
}


In this case you have to add to your references Activeds.dll.

Thursday 19 April 2007

How to use generic ForEach method

You can see in the example how you can use generic ForEach method.

DateTime latestDate = DateTime.MinValue;
string version = string.Empty;

list.ForEach(delegate(Version v)
{
if (v.CreatedAt > latestDate)
{
latestDate = v.CreatedAt;
version = v.Number;
});