Hi ALL,
Can anyone tell me how to validate the text box value? I have one class with function for validating the duplicate value of sql table.
Function:
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
Public Class vDesignationSetup
Dim oFunc As New hrSQLConn.SQLConnection
Dim strINIPath As String = Application.StartupPath + "\BDFCLHR.INI"
'Dim OLEDBConn As New OleDb.OleDbConnection '(strINIPath)
Dim SQLConn As New OleDb.OleDbConnection
Dim oDsDr As OleDb.OleDbDataReader
Dim dsSQL As String
Public Function DesignationNameExists(ByVal sDesignationName As String) As Boolean
Try
dsSQL = "SELECT DesName FROM dbo.hrDesignationSetup WHERE DesName = '" & sDesignationName & "'"
Dim dsCommand As New OleDb.OleDbCommand(dsSQL, SQLConn)
SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
SQLConn.Open()
oDsDr = dsCommand.ExecuteReader()
DesignationNameExists = oDsDr.HasRows
oDsDr.Close()
SQLConn.Close()
Catch ex As Exception
Throw ex
End Try
End Function
Public Function DShortNameExists(ByVal sDShortName As String) As Boolean
Try
dsSQL = "SELECT DesShortName FROM dbo.hrDesignationSetup WHERE DesShortName = '" & sDShortName & "'"
Dim dsCommand As New OleDb.OleDbCommand(dsSQL, SQLConn)
SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
SQLConn.Open()
oDsDr = dsCommand.ExecuteReader()
DShortNameExists = oDsDr.HasRows
oDsDr.Close()
SQLConn.Close()
Catch ex As Exception
Throw ex
End Try
End Function
End Class
Save Button Code:
Private Sub btnedsSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnedsSave.Click
If ValidateData() Then
If vilDesignation.DesignationNameExists(edsDesigName.Text.Trim) Then
MessageBox.Show("Designation Name [" & edsDesigName.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
edsDesigName.Focus()
Exit Sub
ElseIf vilDesignation.DShortNameExists(edsDesShortName.Text.Trim) Then
MessageBox.Show("Designation Short Name [" & edsDesShortName.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
edsDesShortName.Focus()
Exit Sub
ElseIf (edsDefaultCheckbox.Checked) Then
If vilDesignation.CheckBoxValidation(True) Then
MessageBox.Show("Another Designation already set as Default. Please check", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Information)
edsDefaultCheckbox.Focus()
Exit Sub
Else
btnedsSave.Focus()
End If
End If
Try
If bNewData Then
SQLConn.Open()
trns = SQLConn.BeginTransaction
InsertCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show("Record Saved Successfully", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
EnableControlsLoadMode(True)
Else
SQLConn.Open()
trns = SQLConn.BeginTransaction
UpdateCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show("Record Updated Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
EnableControlsLoadMode(True)
End If
Catch ex As Exception
trns.Rollback()
MessageBox.Show("Critical Error!" & ex.Message, "Critical Error.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Problem:
The above code while validating when adding new data is working, but while updating its not working.
Please help.....