Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

[code]
VB
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
[/code]

Save Button Code:

[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

[/code]


Problem:

The above code while validating when adding new data is working, but while updating its not working.

Please help.....
Posted
Updated 27-Dec-11 23:06pm
v2
Comments
uspatel 28-Dec-11 5:07am    
pre tag added.
thatraja 28-Dec-11 5:15am    
What's the error message?

There is lots of code so try to narrow your problem by using the debugger and checking how the problem occurs.

One possibility is that if the check for existing rows isn't working, yuo could have a mismatch in the comparison against the database. One thing is that you should use SqlParameter[^] in your queries instead of concatenated literals in the statement. Also if the problem is that the row isn't found, check your collation settings and if upper and lower case characters are handled as different characters.
 
Share this answer
 
u follow this steps and validating text box.

1. textbox box onblur() call one java script function
2. in that function call client side ajax
3. in that ajax u find that value is there or not in database. u come back with any response.(Y/N)
4. in java script validate that value and show alert boxes.
5.if already exists then focus same testbox
otherwise go to next..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900