Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to make data validation using class. when i am doing this process i couldn't able to make( "Exit Sub" or stop the action) if the data validation result is wrong. please advice me how can i do the process?where i can do the process? if possible, send me the source code.


see the code in the form and its related class

class
VB.NET
Public class datavalidation
Public Function dtavalidation_WHmstr(ByVal pWarehouseID As TextBox, ByVal pName As TextBox) As TextBox
        Try

            If pWarehouseID.Text = "" Or Not IsNumeric(pWarehouseID) Then
                MsgBox("You have not entered the WareHouse ID? This is a mandatory field", MsgBoxStyle.Critical, "Data Validation")
                pWarehouseID.Focus()
            ElseIf pName.Text = "" Then
                MsgBox("You have not entered the WareHouse Name? This is a mandatory field", MsgBoxStyle.Critical, "Data Validation")
                pName.Focus()
            End If
        Catch ex As Exception

        Finally

        End Try
    End Function
  
End Class


This is the form source code
VB
 Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
       
        Dim dtvld As New DataValidation
        dtvld.dtavalidation_WHmstr(TxtWHID, TxtWHName)
end sub
Posted
Updated 24-Jan-12 1:52am
v2
Comments
CPallini 24-Jan-12 7:54am    
What exactly do you need? Could you please state your requirements more clearly?
(at a glance, I guess you may simply throw an exception).
Jαved 24-Jan-12 8:48am    
You are not returning any value then why are you using Functions?

Since you called your class "datavalidation" it's rather hard with the information you have provided what function element needs to do what sort of validation. All that you have provided is a function that returns a TextBox with which you don't even use in your example.

Why not try something along these lines. I am not saying that this is the answer, but rather more food for thought.

VB
Public class datavalidation
Public Function Validate_Fields(ByRef pWarehouseID As TextBox, ByRef pName As TextBox) as BOOLEAN

If pWarehouseID.Text = "" Or Not IsNumeric(pWarehouseID) Then

pWarehouseID.Text = "You have not entered the WareHouse ID? This is a mandatory field."  
pWarehouseID.Focus()
pWarehouseID.Backcolor= Color.Red
return False
            
ElseIf pName.Text = "" Then

pName.Text="You have not entered the WareHouse Name? This is a mandatory field"
pName.Focus()
pName.Backcolor = Color.Red
return False

Else
'Success
return True

End If

End Function


Let me know if this the direction you want to go in. By the way in your pWarehouse_TextChanged reset the backcolor to the default color. Same thing with the pName_Textchanged.

This is not the only way on how to validate your fields. Be creative and have fun with it. :)

Edit: After submitting this answer I did a quick search here on CP and found this article. This might help you out. Keep on coding! :)

Validators for Windows Forms - ValidationProvider Control[^]
 
Share this answer
 
v3
Comments
Jαved 24-Jan-12 8:51am    
seems to be correct.
Edited=> Moved return true in the if else loop.
Hi,

why cant we embed the custom validator to the control itself.
Like if the user goes to another control without filling the first, you can have a tooltip saying this field is manditory or any custom validator.

check this link for validators.

http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp[^]

Hope this helps.
 
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