Friday, December 01, 2006

NullToBoolean, NullToCurrency, NullToDate, NullToInteger, NullToString : VB.NET All Null to datatype functions

Found this very helpfull, just to share.

Public Function NullToBoolean(ByVal vVar As Object, Optional bDefault As
Boolean = False) As Boolean
'Purpose:
' Convert null string to Boolean
'Input:
' vVar - Input variable
'Output:
' Function returns 0 if vVar is null else value
Try
If IsDbNull(vVar) Then
NullToBoolean = bDefault
Else
NullToBoolean = CBool(vVar)
End If
Catch
NullToBoolean = False
End Try
End Function

Public Function NullToCurrency(ByVal vVar As Object) As Decimal
'Purpose:
' Convert null string to currency
'Input:
' vVar - Input variable
'Output:
' Function returns 0 if vVar is null else value
If IsDbNull(vVar) Then
NullToCurrency = 0
Else
NullToCurrency = CDec(vVar)
End If
End Function

Public Function NullToDate(ByVal vVar As Object) As Date
'Purpose:
' Convert null string to currency
'Input:
' vVar - Input variable
'Output:
' Function returns 0 if vVar is null else value
If IsDbNull(vVar) Then
NullToDate = CDate(YEAR_DOT)
Else
NullToDate = CDate(vVar)
End If
End Function

Public Function NullToDateFormat(ByVal vVar As Object, Optional ByVal sFormat
As String = "dd/MMM/yyyy HH:mm") As String
'Purpose:
' Convert null string to currency
'Input:
' vVar - Input variable
' sFormat -
'Output:
' Function returns 0 if vVar is null else value
If IsDbNull(vVar) Then
NullToDateFormat = ""
Else
NullToDateFormat = Format$(CDate(vVar), sFormat)
End If
End Function

Public Function NullToInteger(ByVal vVar As Object) As Integer
'Purpose:
' Convert null string to Integer
'Input:
' vVar - Input variable
'Output:
' Function returns 0 if vVar is null else value
If IsDbNull(vVar) Then
NullToInteger = 0
Else
NullToInteger = CInt(vVar)
End If
End Function

Public Function NullToString(ByVal vVar As Object) As String
'Purpose:
' Convert null string to string
'Input:
' vVar - Input variable
'Output:
' Function returns "" if vVar is null else value
If IsDbNull(vVar) Then
NullToString = ""
Else
NullToString = CStr(vVar)
End If
End Function

[source searchvb]
Add to Blink Add to Furl Add the post to Technorati Favourites Add to Simpy Add to Spurl Add to Yahoo MyWeb Add to del.icio.us Add to Digg It Social bookmark this!
0 comments

Thursday, November 16, 2006

Authenticating LDAP user in VB.NET or ASP.NET

If you want to authenticate user against LDAP using VB.NET or ASP .NET.
Below is the function, you can try. Don't forget to import System.DirectoryServices in your codes.

Public Function AuthenticateUser(ByVal ldapServerName As String, ByVal username As String, ByVal password As String) As Boolean

Dim _ldapServerName = ldapServerName

Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _

"/uid=""" & username & """,ou=yourfullOU", "uid=""" & username & """,ou=yourfullOU", password, AuthenticationTypes.ServerBind)

Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)

Try

Dim mySearchResult As SearchResult = oSearcher.FindOne()

Return True


Catch e As Exception

MsgBox("Error is " & e.Message)

Return False

End Try

End Function
Add to Blink Add to Furl Add the post to Technorati Favourites Add to Simpy Add to Spurl Add to Yahoo MyWeb Add to del.icio.us Add to Digg It Social bookmark this!
0 comments

Wednesday, November 15, 2006

Visual Basic: Format Date And Time

Useful function for VB programmer to format date in standard strings

function ReturnFormattedTime (mytime)
Dim CurrentTime, nHour, nMinute, nSecond
Dim strHour, strMinute, strSecond

CurrentTime = mytime
nHour = Hour( CurrentTime )
nMinute = Minute( CurrentTime )
nSecond = Second( CurrentTime)


strHour = CStr( nHour )
if nHour < 10 then strHour = "0" & strHour

strMinute = CStr( nMinute )
if nMinute < 10 then strMinute = "0" & strMinute

strSecond = CStr( nSecond )
if nSecond < 10 then strSecond = "0" & strSecond

ReturnFormattedTime = strHour & ":" & strMinute & ":" & strSecond

end function



function ReturnFormattedDate (mydate)
Dim CurrentDate, nYear, nMonth, nDay
Dim strYear, strMonth, strDay


CurrentDate = mydate
nYear = Year( CurrentDate )
nMonth = Month( CurrentDate )
nDay = Day( CurrentDate )

strYear = CStr( nYear )

strMonth = CStr( nMonth )
if nMonth < 10 then strMonth = "0" & strMonth

strDay = CStr( nDay )
if nDay < 10 then strDay = "0" & strDay

ReturnFormattedDate = strDay & "/" & strMonth & "/" & strYear

end function


Tag:, , , , ,

Add to Blink Add to Furl Add the post to Technorati Favourites Add to Simpy Add to Spurl Add to Yahoo MyWeb Add to del.icio.us Add to Digg It Social bookmark this!
0 comments