PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Problem in the Boolean type record-VBForums
Results 1 to 9 of 9
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    415

    Resolved [RESOLVED] Problem in the Boolean type record

    Hello VBForums
    Please gentelman .. if you can help me to resolve this problem
    With Button1 i want to save data into my Access database .. but i have an error
    ( incompatible data type in the expression of the criterion )
    When i delete CheckBox1 and CheckBox2 from my code i can save data with success
    In Button1 :
    Code:
       "INSERT INTO GENERALE([IDD],[FATHER_DELIVRER],[MOTHER_DELIVRER],[DELIVRER_OUI],[DELIVRER_NON]) VALUES (@IDD,@FATHER_DELIVRER,@MOTHER_DELIVRER,@DELIVRER_OUI,@DELIVRER_NON)"
    
                        .Parameters.Add("@IDD", OleDbType.Numeric).Value = TextBox1.Text
                        .Parameters.Add("@FATHER_DELIVRER", OleDbType.VarWChar).Value = TextBox2.Text
                        .Parameters.Add("@MOTHER_DELIVRER", OleDbType.VarWChar).Value = TextBox3.Text
                        .Parameters.Add("@DELIVRER_OUI", OleDbType.Boolean).Value = CheckBox1.CheckState
                        .Parameters.Add("@DELIVRER_NON", OleDbType.Boolean).Value = CheckBox2.CheckState
    In my access table I put both fields of the CheckBox type (yes / no)
    Thank you in advance for help
    Cordially
    MADA
    Last edited by MADA BLACK; Jul 11th, 2018 at 02:17 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,938

    Re: Problem in the Boolean type record

    The .CheckState property has 3 possible values: Checked, Unchecked, and Indeterminate (aka "grey")

    The .Checked property has 2 possible values: True, and False


    If you only want True/False, use .Checked instead.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    415

    Re: Problem in the Boolean type record

    Thank you Si_the _geek for help
    In my Form i make this code
    Code:
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
            CheckBox2.Checked = False
        End Sub
        Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
            CheckBox1.Checked = False
        End Sub
    I want only True or False
    How to make ( Checked instead. ) plesa
    MADA

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,938

    Re: Problem in the Boolean type record

    I'm not sure what that code is about... what I meant was for you to change these lines:
    Code:
    .Parameters.Add("@DELIVRER_OUI", OleDbType.Boolean).Value = CheckBox1.Checked
    .Parameters.Add("@DELIVRER_NON", OleDbType.Boolean).Value = CheckBox2.Checked

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    415

    Re: Problem in the Boolean type record

    Thank you master
    I have the same error with this
    Code:
    .Parameters.Add("@DELIVRER_OUI", OleDbType.Boolean).Value = CheckBox1.Checked
    .Parameters.Add("@DELIVRER_NON", OleDbType.Boolean).Value = CheckBox2.Checked
    Cordially
    MADA

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,181

    Re: Problem in the Boolean type record

    This is not related specifically to your issue but both your database and UI appear to be badly designed.

    It appears that your DELIVRER_OUI and DELIVRER_NON columns are just opposites, i.e. if one is Yes then the other is No and vice versa. In that case, you're basically storing one data point in two columns, which is bad. In that case, you should just have one column named DELIVRER and it would be Yes or No.

    In your UI, you then have a few choices. At the moment, you have two CheckBoxes and you appear to manually unchecking one when the other is checked. That should actually be done with RadioButtons, which will automatically uncheck all others in the same group when one is checked. If neither RadioButton is checked by default, you can make sure that one or the other is checked before saving to validate. Other options include a ComboBox with two items and no item selected by default or a single CheckBox with ThreeState set to True and CheckState set to Indeterminate by default.

    As for your issue, it's hard to see how it could still be causing an issue if you have made the change suggested. Can you please post your code as it is now? Please post the whole relevant block (which would include at least the creation of the OleDbCommand) and also please post it formatted clearly for maximum readability, which means, among other things, no huge wads of whitespace at the head of each line. If you hold down the Alt key while dragging in the VS code window, you can select a rectangular block and omit the whitespace at the head of an indented block, as I have shown in the screenshot below. Please make that effort so that it is as easy as possible for us to help you.

    Name:  SelectCodeBlock.jpg
Views: 39
Size:  14.7 KB
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    415

    Re: Problem in the Boolean type record

    Thank you very much jmcilhinney
    Thank you for this great lesson and this explanation .. great from you
    My code is like this :
    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Public Class Form1
        Dim InfoConnection As OleDbConnection
        Dim InfoCommand As OleDbCommand
        Dim InfoAdapter As OleDbDataAdapter
        Dim InfoTable As DataTable
        Dim InfoManager As CurrencyManager
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Delivrer_Connection()
    
            InfoCommand = New OleDbCommand("SELECT * FROM GENERALE ORDER BY IDD", Con2018)
            InfoAdapter = New OleDbDataAdapter()
            InfoAdapter.SelectCommand = InfoCommand
            InfoTable = New DataTable()
            InfoAdapter.Fill(InfoTable)
            DataGridView1.DataSource = InfoTable
    
            TextBox1.DataBindings.Add("Text", InfoTable, "IDD")
            TextBox2.DataBindings.Add("Text", InfoTable, "FATHER_DELIVRER")
            TextBox3.DataBindings.Add("Text", InfoTable, "MOTHER_DELIVRER")
            CheckBox1.DataBindings.Add(New System.Windows.Forms.Binding("CheckState", InfoTable, "DELIVRER_OUI", True))
            CheckBox2.DataBindings.Add(New System.Windows.Forms.Binding("CheckState", InfoTable, "DELIVRER_NON", True))
    
            InfoManager = DirectCast(Me.BindingContext(InfoTable), CurrencyManager)
    
            Timer1.enabled = False
            ProgressBar1.Value = 0
        End Sub
        Private Sub Saving_Prodata()
            Using InfoAdapter As New OleDbDataAdapter("SELECT * FROM GENERALE", Con2018)
                InfoTable = New DataTable
                InfoAdapter.Fill(InfoTable)
                DataGridView1.DataSource = InfoTable
            End Using
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim CheckRecord As String = ""
            Using Check As New OleDbCommand With {.Connection = Con2018, .CommandText = "SELECT IDD FROM GENERALE WHERE IDD  = @IDD "}
                Check.Parameters.Add("@IDD", OleDbType.Numeric).Value = TextBox1.Text
                If Con2018.State = ConnectionState.Closed Then Con2018.Open()
                CheckRecord = Check.ExecuteScalar
                Con2018.Close()
            End Using
            If CheckRecord <> "" Then
                Exit Sub
            Else
                Using Command As New OleDbCommand With {.Connection = Con2018}
                    With Command
                        .CommandText = "INSERT INTO GENERALE([IDD],[FATHER_DELIVRER],[MOTHER_DELIVRER],[DELIVRER_OUI],[DELIVRER_NON]) VALUES (@IDD,@FATHER_DELIVRER,@MOTHER_DELIVRER,@DELIVRER_OUI,@DELIVRER_NON)"
                        .Parameters.Add("@IDD", OleDbType.Numeric).Value = TextBox1.Text
                        .Parameters.Add("@FATHER_DELIVRER", OleDbType.VarWChar).Value = TextBox2.Text
                        .Parameters.Add("@MOTHER_DELIVRER", OleDbType.VarWChar).Value = TextBox3.Text
                        .Parameters.Add("@DELIVRER_OUI", OleDbType.Boolean).Value = CheckBox1.CheckState
                        .Parameters.Add("@DELIVRER_NON", OleDbType.Boolean).Value = CheckBox2.CheckState
                    End With
                    If Con2018.State = ConnectionState.Closed Then Con2018.Open()
                    Dim XXx As String = Command.ExecuteNonQuery
                    If XXx = 1 Then
                        Timer1.Enabled = True
                        Call Saving_Prodata()
                    Else
                        MsgBox("Make sure all data is entered correctly", MsgBoxStyle.MsgBoxRtlReading, "Error")
                    End If
                    Con2018.Close()
                End Using
            End If
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            ProgressBar1.Increment(+5)
            If ProgressBar1.Value = 10 Then
                TextBox4.Text = "Saving ... Wait please"
            End If
            If ProgressBar1.Value = 50 Then
                TextBox4.Text = "Wait please"
            End If
            If ProgressBar1.Value = ProgressBar1.Maximum Then
                TextBox4.Text = "Success of the conservation process"
                Timer1.Stop()
                Timer1.Enabled = False
                MsgBox("Document saved successfully", MsgBoxStyle.MsgBoxRtlReading, "Add process")
            End If
        End Sub
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
            CheckBox2.Checked = False
        End Sub
        Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
            CheckBox1.Checked = False
        End Sub
    End Class
    In my Access DataBase i have 2 fields ( DELIVRER_OUI - DELIVRER_NON ) of type yes / no
    Thank you
    Cordially
    MADA

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,181

    Re: Problem in the Boolean type record

    You still haven't addressed my questions. Yes, you have two columns. Why? It appears that when one of them is True then the other is False so you're not actually storing two data points but only one. Why do you not have a single DELIVRER column that is either True (which would correspond to DELIVRER_OUI being True and DELIVRER_NON being False) or False (which would correspond to DELIVRER_OUI being False and DELIVRER_NON being True)? Also, why are you using CheckBoxes where only one can be checked at a time when that is exactly what RadioButtons are made for?

    If you are going to stick with two CheckBoxes instead of using RadioButtons, this code is wrong:
    vb.net Code:
    1. Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    2.     CheckBox2.Checked = False
    3. End Sub
    4. Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
    5.     CheckBox1.Checked = False
    6. End Sub
    How does it make sense to uncheck the other CheckBox each time a CheckBox is either checked or unchecked? If you're going to write unnecessary code rather than use the correct controls, at least write that code properly. You only want to uncheck the other CheckBox when a CheckBox is checked, NOT when it's unchecked:
    vb.net Code:
    1. Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    2.     If CheckBox1.Checked Then
    3.         CheckBox2.Checked = False
    4.     End If
    5. End Sub
    6. Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
    7.     If CheckBox2.Checked Then
    8.         CheckBox1.Checked = False
    9.     End If
    10. End Sub
    That's still not the real issue though. The real issue is that the way you're doing your data access makes no sense. You populate a DataTable using a data adapter and bind it to your controls (incorrectly by the way, but more on that later) but you then save a record to the database directly and then retrieve it again. That is just wrong. The proper way is to use the data adapter to populate the DataTable and bind that if possible and then, when you want to save changes, you call Update on the same data adapter to save the changes form the DataTable back to the database. I suggest that you follow the CodeBank link in my signature below and check out my thread on Retrieving & Saving Data and check out how to properly use a data adapter.

    As for your data-binding, you were told from the outset that you should be using the Checked property of the CheckBoxes rather than the CheckState and yet you are still binding the CheckState property. The CheckState property should ONLY be used when you want to expressly represent three states. If your database column is not nullable then you only have two states to represent, so you use the Checked property, which can be True or False. If your database column is nullable then you have three states to represent so you use the CheckState property, which can be Checked (represent True), Unchecked (represents False) or Indeterminate (represents NULL).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    415

    Re: Problem in the Boolean type record

    Thank you jmcilhinney
    I followed your instructions and I put a single field and it is perfectly solved
    Thank you for all
    Very nice of you
    Cordially
    MADA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width