October 2007 - Posts

Custom Error Provider (with Error count)

The Error Provider Component used on Windows Forms is a very useful way of displaying the errors on the form, however its missing one small piece of information; the number of errors that are currently on the form. This custom error provider fixes that problem and allows you, among other things, to check the number of errors on the form before performing some action. i.e. If Me.ErrorProvider.HasErrors = False then It's proved very useful in a project I'm helping out on. -------------------------------------------------------------------------------------------------------------------
Public Class ErrorProviderWithCount
Inherits System.Windows.Forms.ErrorProvider

Dim controlsWithErrors As New List(Of Windows.Forms.Control)

Public Sub New(ByVal components As System.ComponentModel.IContainer)
MyBase.New(components)
End Sub

Public Shadows Sub SetError(ByVal control As Windows.Forms.Control, ByVal value As String)
MyBase.SetError(control, value)
If String.IsNullOrEmpty(value) Then
If controlsWithErrors.Contains(control) = True Then
controlsWithErrors.Remove(control)
End If
Else
If controlsWithErrors.Contains(control) = False Then
controlsWithErrors.Add(control)
End If
End If
End Sub

Public ReadOnly Property Count() As Integer
Get
Return controlsWithErrors.Count
End Get
End Property

Public ReadOnly Property HasErrors() As Boolean
Get
Return controlsWithErrors.Count > 0
End Get
End Property

End Class
Posted by dsmyth
Filed under:

XmlResolver doesn't authenticate with Proxy server

Just had a problem in resolving a DTD, stored at a remote URI, while loading an XML file into a XmlDocument. Usually a XmlUrlResolver is used to resolve remote XSD or DTD defined in an XML document, however the XmlUrlResolver doesn't perform Proxy authentication meaning that as soon as you try to load the document a WebException is raised saying "The remote server returned an error: (407) Proxy Authentication Required.". The solution is to create your own XmlResolver that handles proxy authentication and here it is.

Public Class XmlProxyUrlResolver
Inherits XmlResolver

Private m_proxy As IWebProxy = WebProxy.GetDefaultProxy()
Private m_credentials As ICredentials

Public Overrides WriteOnly Property Credentials() As System.Net.ICredentials
Set(ByVal value As System.Net.ICredentials)
m_credentials = value
End Set
End Property

Public Overrides Function GetEntity(ByVal absoluteUri As System.Uri, ByVal role As String, _
ByVal ofObjectToReturn As System.Type) As Object
Dim request As WebRequest = WebRequest.Create(absoluteUri)
request.Proxy = m_proxy
request.Proxy.Credentials = m_credentials
request.Credentials = m_credentials
Return request.GetResponse().GetResponseStream()
End Function

End Class


The XmlProxyUrlResolver resolves the DTD at http://www.w3.org with authenticating with the proxy server.

Posted by dsmyth
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.