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

I have one vb form with two Textbox, one Checkbox, and two buttons. I am using SQL Database as back end for this form. Controls of my form goes like that:

1. txtGenderID
2. txtGenderName
3. chkIsDefault
4. btnSave
5. btnCancel

SQL Table as follows:

Table Name: tblGender
VB
GsGenderCode char(4)
GsGenderName varchar(50)
IsDefault bit (0 = false, 1=true)
SetDate datetime

In this form I want to make some events on "chkIsDefault", I want this check box to validate true value.

i.e. if the user has already set one record as true and if user set another value as true then it should validate saying that "True value has already been set.". I did coding but not working.

My Code are as follow:

Function
-------------------------------------------------------------------------------
[code]
VB
Public Function CheckBoxValidation(ByVal sCheckBoxCheck As Boolean) As Boolean
 Try
 Dim oCtsDr As OleDb.OleDbDataReader
 Dim CtsSQL As String = "SELECT IsDefault FROM dbo.tblGender WHERE IsDefault = '" & sCheckBoxCheck & "'"
 Dim CtsCommand As New OleDb.OleDbCommand(CtsSQL, SQLConn)
 SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
 SQLConn.Open()
 oCtsDr = CtsCommand.ExecuteReader()
 CheckBoxValidation = oCtsDr.HasRows
 oCtsDr.Close()
 SQLConn.Close()
 Catch ex As Exception
 Throw ex
 End Try
 End Function
[/code]
-------------------------------------------------------------------------------
btnSave Code
[code]
VB
Private Sub btnCtsSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCtsSave.Click
 If ValidateData() Then
 If vilGender.CheckBoxValidation(True) Then
 MessageBox.Show("Another Gender already set as Default. Please check", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Information)
 ctsDefaultCheckBox.Focus()
 Exit Sub
 Else
 btnCtsSave.Focus()
 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]
-------------------------------------------------------------------------------

CAN ANYONE TELL ME WHERE DID I WENT WRONG? WHEN I CLICK BUTTON IT VALIDATES BOTH THE VALUE [TRUE AND FALSE].
(Do not use all capital letters.)
Can anyone tell me where did I went wrong? When I click button it validates both the value[True and False].
Posted
Updated 13-Dec-11 19:57pm
v2

VB
vilGender.CheckBoxValidation(True)
is always sending true. So is the validation is happening for both checked and unchecked state of the checkbox.

Instead, you may checkfirst on the checkbox value before you invoke the validation like
VB
if(chkIsDefault.Checked)Then
If vilGender.CheckBoxValidation(True)


Cheers,
Balaji
 
Share this answer
 
Thank you very much, you are genius. It worked fine now. How can do the same validation for textbox value.

Function as follows:
[code]
Public Function GenderNameExists(ByVal sGenderName As String) As Boolean
Try
Dim oCtsDr As OleDb.OleDbDataReader
Dim CtsSQL As String = "SELECT Gender FROM dbo.hrGenderSetup WHERE Gender = '" & sGenderName & "'"
Dim CtsCommand As New OleDb.OleDbCommand(CtsSQL, SQLConn)
SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
SQLConn.Open()
oCtsDr = CtsCommand.ExecuteReader()
GenderNameExists = oCtsDr.HasRows
oCtsDr.Close()
SQLConn.Close()
Catch ex As Exception
Throw ex
End Try
End Function
[/code]

I have called this funcation in save button as follows:

[code]
If ValidateData() Then
If vilContacts.GenderNameExists(Gender.Text.Trim) Then
MessageBox.Show("Gender [" & Gender.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Gender.Focus()
Exit Sub
ElseIf (ctsDefaultCheckBox.Checked) Then
If vilContacts.CheckBoxValidation(True) Then
MessageBox.Show("Another Title already set as Default. Please check", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Information)
ctsDefaultCheckBox.Focus()
Exit Sub
Else
btnCtsSave.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]

The above code is working only when i add new record, but when i edit existing record it does not work.
 
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