Hi, thought it time i stopped lurking :) Im currently building a project in .net and im having issues with check boxes. On one form i have a few text boxes for username, password, initials. Then check boxes to tick (these will be to allow/deny access to various forms later) this info is then saved to SQL database using a query. The check box values saving as 1 or 0, it being ticked or not.

Now on another form for existing users, i have identical check boxes, these ones however have to be bound, so i have a databind for "checkstate" set in the properties for each one. This works fine when i test the form - if they where ticked when creating the user they show as ticked, or not, on the edit form...

However, if i then hit the "save" button on this form, i get an error. The update tries to save the checkbox values as "true" or "false" and not 0 or 1. I cant for the life in me see how to set the checked value = 1, unchecked value = 0 properties. You can do that on check boxes in a gridview, but i cant see how on regular check boxes.

its infuriating, i cant see why one set of checkboxes saves the state as 1/0 and the other as true/false. They are actualy a copy and paste of each other, just that they write to the DB differently, and one set is bound.

Any ideas?

Checked property is Boolean type. VB.NET project uses a compiler settings - Option Strict = On/Off. If Strict is on then you can't assign value 1 or 0 to the Checked property.

>update tries to save the checkbox values as "true" or "false" and not 0 or 1..
Update to a table (database) is a problem due to datatype mismatch of fields.

Thanks for the reply!

Yes, thats whats happening though. When i use one form to create users it saves the checkbox state as 1 or 0 - it being ticked or not.

However, when i use another form to edit an existing user record, because those checkboxes are bound controls (so they show as ticked or not depending) - when you then save/update that user it saves the checkbox state as "True" or "False"

So what you are saying is, i need to stop it creating records with 1 or 0 set? I dont see where to change this, thats the problem, in Gridview a checkbox properties you can set the value for checked and unchecked. "1" and "0" or "apples" and "oranges" etc. Theres no option to do this with checkboxes outside the gridview though - at least not that ive found. Im sure you could set that in VS 2005 though :S

Strict isnt on. And using VS.NET 2008

heres the code im using to create the db entry, its "checkstate" thats at fault then it seems, what i cant figure out is what i should use instead of checkstate? if i can get this to use true/false instead of 1/0 then it solves my problem, i cant figure out the syntax however:

If String.IsNullOrEmpty(UsernameTb.Text) Then
            MsgBox("You must enter a Username!")
        Else
            If String.IsNullOrEmpty(InitialsTb.Text) Then
                MsgBox("You must enter users Initials!")
            Else
                If String.IsNullOrEmpty(PasswordTb.Text) Then
                    MsgBox("You must enter a Password!")
                Else
                    Try
                        Me.UsersTableAdapter.AddUserQry(UsernameTb.Text, InitialsTb.Text, PasswordTb.Text, ApplicationsChkbx.Checkstate, AdministrationChkBx.Checkstate, CustomerSearchChkBx.Checkstate, CustomerFormChkbx.Checkstate, UnderWrittingChkBx.Checkstate)
                        'Update the Gridview
                        Me.UsersTableAdapter.Fill(Me.WDA_SQLDataSet.Users)
                        'Empty all the text boxes and set check boxes back to unticked state
                        MsgBox("Created user " & UsernameTb.Text)

                        UsernameTb.Text = Nothing
                        InitialsTb.Text = Nothing
                        PasswordTb.Text = Nothing
                        AdministrationChkBx.CheckState = 0
                        ApplicationsChkbx.CheckState = 0
                        CustomerFormChkbx.CheckState = 0
                        CustomerSearchChkBx.CheckState = 0
                        UnderWrittingChkBx.CheckState = 0

                    Catch ex As Exception
                        MsgBox("Error will robinson, error")
                    End Try
                End If
            End If
        End If

CheckBox1.CheckState is an enum type (Integer) (0/1/2) - and CheckBox1.Checked is boolean property (true/false).

What are the fields (columns) datatype?

ahh i was just playing with this. checked worked, however then when opening the form with a gridview on i get an error saying its not a valid boolean. The error keeps popping up for each and every check box in the gridview and i have to kill the program.

At the moment the datatype is set to varchar(5)

ok on changing it to checkboxname.checked

its now saving as 0 for unticked and -1 for ticked. not 1 or 0, but -1 and 0

any suggestions?

as a bit of a workaround i tried changing a text box text when the checkbox is ticked or not. Default text in the text box is set to "False"

Private Sub AdministrationChkBx_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdministrationChkBx.CheckedChanged
        If AdministrationChkBx.Checked = True Then
            AdministrationTB.Text = "True"
        Else
            AdministrationTB.Text = "False"
        End If
    End Sub

Then changed the insert query to save that instead.

Me.UsersTableAdapter.AddUserQry(UsernameTb.Text, InitialsTb.Text, PasswordTb.Text, ApplicationsTB.Text

however, when i hit save, nothing happens, it wont write to SQL. i dont even get an error.

im totaly stumped by this now, any help much appreciated!

Can I see the table struct along with datatype?

sure,

query being used:

INSERT INTO [Users] ([Username], [Initials], [Password], [Applications_Access], [Administration_Access], [CustomerSearch_Access], [CustomerForm_Access], [Underwritting_Access]) VALUES (@Username, @Initials, @Password, @Applications_Access, @Administration_Access, @CustomerSearch_Access, @CustomerForm_Access, @Underwritting_Access);
SELECT ID, Username, Initials, Password, Applications_Access, Administration_Access, CustomerSearch_Access, CustomerForm_Access, Underwritting_Access FROM Users WHERE (ID = SCOPE_IDENTITY())

This do for the table struct? i just pulled it out of SQL query editor:

INSERT INTO [WDA_SQL].[dbo].[Users]
           ([Username]
           ,[Initials]
           ,[Password]
           ,[Administration_Access]
           ,[Applications_Access]
           ,[CustomerForm_Access]
           ,[CustomerSearch_Access]
           ,[Underwritting_Access]
           ,[UserNotes])
     VALUES
           (<Username, varchar(20),>
           ,<Initials, varchar(4),>
           ,<Password, varchar(20),>
           ,<Administration_Access, varchar(5),>
           ,<Applications_Access, varchar(5),>
           ,<CustomerForm_Access, varchar(5),>
           ,<CustomerSearch_Access, varchar(5),>
           ,<Underwritting_Access, varchar(5),>
           ,<UserNotes, varchar(200),>)
GO

Solved this now, it should be a bit field in database, not varchar.

Also, Checkboxname.Checked.Tostring would work too.
Just updating incase anyone else searches and finds this in future, thanks for the help adatapost :)

Thanks,

Mark this thread as Solved if you get solution.

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.