Checkbox and database

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
I have a database with a yes/no column. In my app I have a check box. Does anyone know how I can have the checkbox to check if the column in the database is yes and uncheck if it is no? It's an Access database.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Ok I have done this now, but its not very eloquent

To get the checkbox to check I use this piece of code.
VB.NET:
[size=2][color=#008000]'checks if the student has been placed and checks box accordingly
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2]i = reader.Item("Placed")
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] i = [/size][size=2][color=#0000ff]True[/color][/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].CheckBoxPlaced.Checked = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].CheckBoxPlaced.Checked = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If[/color][/size]
[size=2][color=#0000ff]
[/color][/size]

And to update the database with a check I use this code
VB.NET:
[/color][/size]
[size=2][color=#0000ff][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][size=2][color=#000000] i [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].CheckBoxPlaced.Checked = [/size][size=2][color=#0000ff]True[/color][/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]i = "True"
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]i = "False"
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] cmd [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDbCommand = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDbCommand
 
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].OleDbConnection1.Open()
 
cmd.Connection = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].OleDbConnection1
 
cmd.CommandType = CommandType.Text
 
cmd.CommandText = "UPDATE Student SET Placed =" & [/size][size=2][color=#0000ff]CBool[/color][/size][size=2](i) & ", County = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxCounty.Text & "', Forename = '" & [/size]
[size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxForename.Text & "', HouseNumber = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxHouseNumber.Text & "', Location = '" & [/size]
[size=2][color=#0000ff]Me[/color][/size][size=2].ComboBoxLocation.SelectedItem & "', Jobtype = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].ComboBoxJobType.SelectedItem & "', Postcode = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxPostCode.Text & "', Salutation = '" & [/size]
[size=2][color=#0000ff]Me[/color][/size][size=2].ComboBoxSalutation.SelectedItem & "', Street = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxStreet.Text & "', Surname = '" & [/size]
[size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxSurname.Text & "', TelephoneNumber = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxTelephoneNo.Text & "', TownCity = '" & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxTownCity.Text & "' WHERE (EnrolmentNumber = " & [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxEnrolmentNo.Text & ")"
 
sqlreturn = cmd.ExecuteNonQuery
 
[/size][/color][/size][size=2][color=#0000ff]
 
[/color][/size][/color][/size][size=2][color=#0000ff]

If anyone has a better way of doing this then please do tell
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Looks fine to me, 3 things you might want to change though:

1. Change i to type bool
2. In If statements on bool's you don't need the '= true', that includes .checked as that returns a bool val.
3. Don't open the DB Connection untill your ready to execute
 
Top Bottom