Archive

Archive for March, 2009

Preview of Microsoft Ajax 4.0 available for download

March 31st, 2009 Anuraj P No comments

The Microsoft Ajax team made the fourth preview of the 4.0 version available on CodePlex. The AJAX functionality in ASP.NET 4.0 Preview 4 enables new client data scenarios for page and component developers that allow JSON data from the server to be rendered as HTML in a highly manageable and efficient way.

You can get more information about ASP.Net AJAX 4.0 Preview from codeplex.

Categories: .Net 3.0 / 3.5, ASP.Net, Javascript Tags:

How to validate email addess in Windows Forms

March 24th, 2009 Anuraj P 6 comments

Today I want to add validation for email address in a Windows Forms application. I searched google, but didn’t got any simple solution. Then I tried with ASP.Net regex validation control and took the validation expression from it used in RegEx class.

Here is code

static bool IsValidEmail(string Email)
{
Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
return regex.IsMatch(emailAddress);
}

Happy Coding [:)]

Getting and Setting properties on runtime using Reflectoion

March 9th, 2009 Anuraj P No comments

In series of Reflection related posting this is the second post. You can see the post here. Now I am posting about Properties. We can set / read properties of Class using reflection.

Setting Properties

1) For setting properties, we enumerate all the properties using Type.GetProperties() method
2) Then we will check, whether the property is readonly or not.
3) Read the value(if the datatype of property and the datatype of the value you are trying to set, didn’t match it will throw some error.)
4) If datatype is mismatching, convert the value to property data type using “Convert.ChangeType” *
5) Call the PropertyInfo.SetValue method.

Here is the source code, here I am reading from Registry and Loads the required data in a class level variable.

Dim result As MyCustomClass
Dim PropertyValue As Object
Dim converter As New TypeConverter
For Each CurrentProperty As PropertyInfo In MyType.GetProperties
If CurrentProperty.CanWrite Then
PropertyValue = GetValue(CurrentProperty.Name)	'GetValue function retuns the value based on the parameter given.
If PropertyValue IsNot Nothing Then
	CurrentProperty.SetValue(result, Convert.ChangeType(PropertyValue, CurrentProperty.PropertyType), Nothing)
End If
End If
Next

* Convert.ChangeType – Works only with basic datatypes.

And for getting we will do almost reverse operation.
Getting values from Property

It is simple and strait forward, and here is the code

Dim propertyValue As String
For Each CurrentProperty As PropertyInfo In MyType.GetProperties
If CurrentProperty.CanRead Then
propertyValue = CurrentProperty.GetValue(MyCustomClass, Nothing).ToString
End If
Next

Happy Reflection.

Reading POP3 mailbox using VB.Net

March 2nd, 2009 Anuraj P No comments

Few days back I got an R&D from my friend, it is like, in Blogger, we can send mails to a specific mail address and we a can Post. He want the same implementation in asp.net, but the mail Id will be static. So I thoght of reading POP3 mailbox using .Net. And I didn’t done the same in ASP.Net, but I created a POC in VB.Net, which will help you to check you POP3 account.

Dim _tcpClient As New TcpClient
        Dim _networkStream As NetworkStream
        Dim _Msg As String
        With _tcpClient
            .Connect(Me.txtServerIp.Text, Integer.Parse(Me.txtPortNum.Text))
            _networkStream = .GetStream
            Dim sw As New StreamWriter(_networkStream)
            Dim sr As New StreamReader(_networkStream)
            If Not CheckError(sr.ReadLine()) Then
                sw.WriteLine(String.Format("USER {0}", Me.txtUsername.Text))
                sw.Flush()
            End If

            If Not CheckError(sr.ReadLine()) Then
                sw.WriteLine(String.Format("PASS {0}", Me.txtPassword.Text))
                sw.Flush()
            End If

            If Not CheckError(sr.ReadLine()) Then
                sw.WriteLine("STAT ")
                sw.Flush()
            End If
            _Msg = sr.ReadLine
            Dim MsgCount As String = _Msg.Split(New String() {" "},  _
StringSplitOptions.RemoveEmptyEntries)(1)
            If Integer.Parse(Me.lblMsgCount.Text) < Integer.Parse(MsgCount) Then
                Me.lblMsgCount.Text = MsgCount
            End If
            sw.WriteLine("Quit ")
            sw.Flush()
            sw.Close()
            sr.Close()
            _networkStream.Close()
            _tcpClient.Close()
        End With

And the check error method will check for the ERR status and display a generic error messgae.

Private Function CheckError(ByVal Msg As String) As Boolean
        Dim result As Boolean = False
        If Msg.IndexOf("+OK") >= -1 AndAlso Msg.IndexOf("-ERR") >= 0 Then
            MessageBox.Show(My.Resources.UI.Msg_GenericError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            result = True
            Me.tmr.Enabled = False
        End If
        Return result
    End Function

I am planning to write a simple utility using this code, and to host it on codeplex, which will give the count of unread mails in your mailbox.