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
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
-------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------
btnSave Code
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
-------------------------------------------------------------------------------
CAN ANYONE TELL ME WHERE DID I WENT WRONG? WHEN I CLICK BUTTON IT VALIDATES BOTH THE VALUE [TRUE AND FALSE].