Wednesday, March 17, 2010

Common Functions Used in VB.Net

/* This function is to Check AlphaNumeric Charcaters */

Public Function IsAlphanumeric(ByVal strValue As String) As Boolean
Dim lngLength As Long
Dim lngCounter As Long
Dim intAsciiVal As Integer
IsAlphanumeric = True
strValue = UCase(strValue)
lngLength = Len(strValue)
For lngCounter = 1 To lngLength
intAsciiVal = Asc(Mid(strValue, lngCounter, 1))
If (intAsciiVal >= 48 And intAsciiVal <= 57) Or (intAsciiVal >= 65 And intAsciiVal <= 90) Then Else IsAlphanumeric = False Exit For End If Next End Function /*This function is used to Display Error Message.*/

Public Sub gnErrorMessage(ByVal psMsg As String, ByVal psTitle As String, Optional ByRef poCtrl As Object = Nothing)
Dim Response, DgDef, Msg, Title As Object
Dim i As Short
Call MessageBox.Show(psMsg, psTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
If Not IsNothing(poCtrl) Then
If poCtrl.Enabled = True And poCtrl.Visible = True Then
poCtrl.focus()
If TypeOf poCtrl Is TextBox Or TypeOf poCtrl Is ComboBox Then poCtrl.selectall()
End If
End If
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
End Sub

/*This function is used to concatenate quotes before the character string.

Public Function gsTextConvert(ByVal vsStr As String) As String
If IsNothing(vsStr) Then
vsStr = ""
Else
vsStr = Trim(vsStr)
End If
If vsStr.Length <> 0 Then
gsTextConvert = "'" & Replace(vsStr, "'", "''") & "'"
Else
gsTextConvert = "NULL"
End If
End Function

/*This function is used to Convert Value in TextBox Into Upper Case.*/

Public Sub gnToUpper(ByRef pCtrl As System.Windows.Forms.TextBox)
Dim pviCurrPos As Integer
pviCurrPos = pCtrl.SelectionStart
pCtrl.Text = UCase(pCtrl.Text)
pCtrl.SelectionStart = pviCurrPos
End Sub

/*This function is used to Check The Value Should Not Be Empty */

Public Overloads Function gbEmptyChk(ByRef roCtrl As System.Windows.Forms.Control, ByVal vsMsg As String) As Boolean
Dim pvsStr As String
gbEmptyChk = True
pvsStr = Trim(roCtrl.Text)
If pvsStr = "" Then
vsMsg = vsMsg & " cannot be empty."
Call gnErrorMessage(vsMsg, "Validation", roCtrl)
Exit Function
End If
gbEmptyChk = False
End Function

/*This Function checks that the Combo Box Should Not Be Empty */

Public Overloads Function gbEmptyChk(ByRef roCtrl As System.Windows.Forms.ComboBox, ByVal vsMsg As String) As Boolean
Dim pvsStr As String
gbEmptyChk = True

If roCtrl.SelectedIndex = -1 Or Trim(roCtrl.Text).Length < 1 Then pvsStr = vsMsg & " cannot be empty." Call gnErrorMessage(pvsStr, "Validation", roCtrl) Exit Function End If gbEmptyChk = False End Function /* This subroutine is to make a connection */

Public Sub gnMakeConnection()
'//Try
gvoAdoCon = New SqlConnection()
gvoAdoCon.ConnectionString = gvsConnectionString
gvoAdoCon.Open()

''''Catch e As Exception
'''' Call gnErrorMessage(e.Message, "Make Connection")
''''End Try
End Sub


/* This subroutine is to end a connection */

Public Sub gnEndConnection()
'//Try
gvoAdoCon.Close()
gvoAdoCon = Nothing
''''Catch e As Exception
'''' Call gnErrorMessage(e.Message, "End Connection")
''''End Try
End Sub

/* This subroutine is to Begin a Transaction */

Public Function goBeginTran() As SqlClient.SqlCommand
Dim pvuCommand As New SqlClient.SqlCommand()
'''Try
gvoAdoTran = gvoAdoCon.BeginTransaction()
pvuCommand.Transaction = gvoAdoTran
goBeginTran = pvuCommand
''''Catch e As Exception
'''' Call gnErrorMessage(e.Message, "Begin Tran")
''''End Try
End Function

/* This subroutine is to Commit a Transaction */
Public Sub gnCommitTran()
Try
gvoAdoTran.Commit()
Catch ex As Exception
'// no error handling
'''' Call gnErrorMessage(e.Message, "Commit Tran")
End Try
End Sub

/* This subroutine is to Rollback a Transaction */

Public Sub gnRollbackTran()
Try
'//gvoAdoTran.Connection.tr
gvoAdoTran.Rollback()
Catch ex As Exception
'//no error handling
'''' Call gnErrorMessage(e.Message, "Rollback Tran")
End Try
End Sub

/* This function is to return a DataReader */

Public Function goPopulateDataReader(ByVal vsSQL As String, ByVal vbTran As Boolean) As SqlDataReader
Dim pvoCmd As New SqlCommand()
''''Try
With pvoCmd
.Connection = gvoAdoCon
.CommandText = vsSQL
If vbTran Then .Transaction = gvoAdoTran
goPopulateDataReader = .ExecuteReader
End With
''''Catch e As Exception
'''' Call gnErrorMessage(e.Message, "goPopulateDataReader")
''''End Try
End Function

/* This Subroutine is to Work With DataSet */

Public Sub gnPopulateDataSet(ByVal vsSQL As String, ByVal vsName As String, ByRef roDataSet As DataSet)
Dim pvoAdapt As SqlDataAdapter
''''Dim pvsSQL As String
''''Try
roDataSet = New DataSet()
pvoAdapt = New SqlDataAdapter(vsSQL, gvoAdoCon)
pvoAdapt.Fill(roDataSet, vsName)
''''Catch e As Exception
'''' Call gnErrorMessage(e.Message, "goPopulateDataSet")
''''End Try
End Sub

/* This Subroutine is to Execute a Transaction */

Public Sub gnExecuteSQL(ByVal vsSQL As String, ByVal vbTran As Boolean)
Dim pvoCmd As New SqlCommand()
'''Try
With pvoCmd
.Connection = gvoAdoCon
.CommandText = vsSQL
If vbTran = True Then .Transaction = gvoAdoTran
.ExecuteNonQuery()
End With
''''Catch e As Exception
'''' Call gnErrorMessage(e.Message, "goExecuteSQL")
''''End Try
End Sub


/* This Function is to check Duplicate Entry */

Public Function gbDuplicateCheck(ByVal vsString As String, ByVal vsCtrlName As String, ByRef roCtrl As System.Windows.Forms.Control) As Boolean
Dim pvoDR As SqlDataReader
Dim pviCount As Short
gbDuplicateCheck = True
If gbEmptyChk(roCtrl, vsCtrlName) Then Exit Function
pvoDR = goPopulateDataReader(vsString, False)
pvoDR.Read()
pviCount = pvoDR(0)
pvoDR.Close()
pvoDR = Nothing
If pviCount > 0 Then
gbDuplicateCheck = True
Call gnErrorMessage(vsCtrlName & " already exists.", "Duplicate Check", roCtrl)
Exit Function
Else
gbDuplicateCheck = False
End If
End Function

/* This subroutine is used to Remove Space */

Public Sub gnRemoveSpace(ByRef pForm As System.Windows.Forms.Form)
Dim lCtrl As Object
For Each lCtrl In pForm.Controls
If TypeOf lCtrl Is System.Windows.Forms.TextBox Then
lCtrl.Text = Trim(lCtrl.Text)
End If
Next lCtrl
End Sub

/* To set all the combobox values as blank in the form */

Private Sub gnInitializeComboControl(ByRef roControl As Control)
Dim pvuControl As Control
For Each pvuControl In roControl.Controls
If pvuControl.Controls.Count > 1 Then
Call gnInitializeComboControl(pvuControl)
Else
If TypeOf pvuControl Is ComboBox Then
CType(pvuControl, ComboBox).SelectedIndex = -1
CType(pvuControl, ComboBox).SelectedIndex = -1
End If
End If
Next
End Sub

/* To Enable/Disable Controls In One Form Throgh Other Form */

Public Sub gnEnableDisableFields(ByRef roForm As System.Windows.Forms.Form)
Dim pvuControl As Control
Dim pvuInnrControl As Control
For Each pvuControl In roForm.Controls
'//If TypeOf pvuControl Is GroupBox Then
If pvuControl.Controls.Count > 1 Then
For Each pvuInnrControl In pvuControl.Controls
If Not TypeOf pvuInnrControl Is Label Then pvuInnrControl.Enabled = False
Next
Else '//If TypeOf pvuControl Is TextBox Then
If Not TypeOf pvuControl Is Label Then pvuControl.Enabled = False
End If
Next
End Sub

/* Allow AlphaNumeric on KeyPress Event in a Text Box */

Public Sub gnAllowAlphaNumeric(ByRef roKeyChar As KeyPressEventArgs)
Select Case Asc(roKeyChar.KeyChar)
Case Asc("0") To Asc("9") '// for between 0 to 9
Case Asc("A") To Asc("Z") '// for between A to Z
Case Asc("a") To Asc("z") '// for between a to z
Case 8 '//backspace
Case Else
roKeyChar.Handled = True
End Select
End Sub

/* Allow Numeric on KeyPress Event in a Text Box */

Public Sub gnAllowNumeric(ByVal vsText As String, ByRef roKeyChar As KeyPressEventArgs, ByVal vbDecimal As Boolean)
Select Case Asc(roKeyChar.KeyChar)
Case Asc("0") To Asc("9") '// for between 0 to 9
Case 8 '//backspace
Case 46 And vbDecimal '// decimal
If InStr(vsText, ".", CompareMethod.Text) > 0 Then roKeyChar.Handled = True
Case Else
roKeyChar.Handled = True
End Select
End Sub

No comments:

Post a Comment