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.....

Recommended Answers

All 10 Replies

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

You posted everything except the code that's giving you the problem.
Where is the UpdateCMD code?

The other part of this is very misleading.
Based on your message title and your opening sentence, I thought the problem was in the validation.

Thansk for the response, well the updateCMD and insertCMD is coded in separate class. The problem which i am facing is with the validation of textbox while updating existing records of the database. While inserting new record there is not problem.

please help me...

You posted everything except the code that's giving you the problem.
Where is the UpdateCMD code?

The other part of this is very misleading.
Based on your message title and your opening sentence, I thought the problem was in the validation.

Cant you do it at the back end? it will be easy for you.

Can you tell me how?

Cant you do it at the back end? it will be easy for you.

You need to write stored procedure which accepts ur edsDesigName.Text.Trim as input. and check if it is already exist in ur table and retur true or false accordingly.
If result is false give the msg to users saying its already exists.

Thanks for your guide, can you please show me the sample?

You need to write stored procedure which accepts ur edsDesigName.Text.Trim as input. and check if it is already exist in ur table and retur true or false accordingly.
If result is false give the msg to users saying its already exists.

Search in google for Stored procedure with out put parameter.. u will get many examples.

Is there any other solution for my post... Plesae help me...

Cant you write Procedure with return values to validate?

Hi,

I would appreciate if you can show me how to? Since am very new to this and whatever application which i am developing now is my first project.

Please helpe me...

Cant you write Procedure with return values to validate?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.