Results 1 to 9 of 9

Thread: [RESOLVED] Save boolean value to MS Database

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Resolved [RESOLVED] Save boolean value to MS Database

    hi,
    i have problem when save boolean value to database.
    this my code :
    vb.net Code:
    1. if bAdd then
    2.      scmd.CommandText = "insert into products(prod_id,status) values(@prod_id,@status)"
    3. else
    4.      scmd.CommandText = "update products set status=@status where prod_id=@prod_id"
    5. end if
    6.  
    7. scmd.Parameters.Add(New OleDbParameter("@status", CBool(chkStatus.Checked)))
    8.  
    9. scmd.Connection = dbConn
    10. Dim iResult As Integer = scmd.ExecuteNonQuery
    i get problem when execute query at line
    Code:
    scmd.ExecuteNonQuery
    Code:
    Data type mismatch in criteria expression
    Last edited by Tengkorak; Apr 1st, 2011 at 11:33 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Save boolean value to MS Database

    First up, there's no point using CBool in that code because the Checked property of a CheckBox is already type Boolean.

    As for the question, it relates to how your parameters are being handled. Even though Access/Jet/OleDb allows you to use names for your parameters, that is only for your benefit. The names are not actually used internally. It's only the position of the parameters that matter. You have a problem because in your INSERT statement @prod_id comes BEFORE @status and in the UPDATE statement it comes AFTER. That means that, for one or the other, the parameters you add will be the wrong way around.

    Whatever order the parameters appear in the SQL code is the order you must add them to the command. You need to either put the parameters in the same order in both SQL statements or else add the parameters in a different order depending on the SQL statement.
    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

  3. #3

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Save boolean value to MS Database

    thank for your reply
    but I still haven't been able to store the boolean data.
    I've change my code but still haven't been able too.

    just add the information.
    I use the microsoft access database, the data type of the field status is YES/NO

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Save boolean value to MS Database

    How can we tell you what's wrong with the new code if you don't show it to us?
    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

  5. #5

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Save boolean value to MS Database

    this my complate code :
    vb.net Code:
    1. If bAdd Then
    2.                 scmd.CommandText = "insert into inv_t_products (prod_id,prod_name,unit,unitprice,id_group_prod,supp_id,konsinyasi) " & _
    3.                         "values(@prodid,@prodname,@unit,@unitprice,@idgroup,@idsupp,@konsinyasi)"
    4.  
    5.                 scmd.Parameters.Add(New OleDbParameter("@prodid", txtKd.Text))
    6.  
    7.  
    8.                 'Debug.Print("Insert Mode")
    9.             Else
    10.                 'modify
    11.                 scmd.CommandText = "update inv_t_products set prod_name=@prodname,unit=@unit,unitprice=@unitprice, " & _
    12.                     "id_group_prod=@idgroup,supp_id=@idsupp ,konsinyasi=@konsinyasi " & _
    13.                     "where prod_id='" & txtKd.Text & "'"
    14.  
    15.  
    16.             End If
    17.  
    18.  
    19.             scmd.Parameters.Add(New OleDbParameter("@prodname", txtNm.Text))
    20.             scmd.Parameters.Add(New OleDbParameter("@unit", txtSat.Text))
    21.             scmd.Parameters.Add(New OleDbParameter("@unitprice", txtHarga.Text))
    22.             scmd.Parameters.Add(New OleDbParameter("@idgroup", t_kategori))
    23.             scmd.Parameters.Add(New OleDbParameter("@idsupp", t_supplier))
    24.             'scmd.Parameters.Add(New OleDbParameter("@konsinyasi", chkKonsy.Checked))
    25.  
    26.             Dim stsPar As New OleDbParameter
    27.             stsPar.OleDbType = OleDbType.Boolean
    28.             stsPar.ParameterName = "konsinyasi"
    29.             stsPar.Value = chkKonsy.Checked
    30.  
    31.             scmd.Parameters.Add(stsPar)
    32.  
    33.            
    34.             scmd.Connection = dbConn
    35.             Dim iResult As Integer = scmd.ExecuteNonQuery

    but data not saved and get same error.

  6. #6
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: Save boolean value to MS Database

    Instead of using this

    vb Code:
    1. scmd.Connection = dbConn
    2.       Dim iResult As Integer = scmd.ExecuteNonQuery

    Try this

    vb Code:
    1. scmd.Connection = dbConn
    2.        scmd.ExecuteNonQuery()

  7. #7

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Save boolean value to MS Database

    vb Code:
    1. Dim iResult As Integer = scmd.ExecuteNonQuery

    if value of iResult = 1 means execute success and 0 is not.

    my problem at field konsinyasi
    vb Code:
    1. stsPar.ParameterName = "konsinyasi"

    i can not save to YES/NO data type at ms access database.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Save boolean value to MS Database

    You have ignored what I said in my previous post. I said that you must add the parameters to the command in the same order that they appear in the SQL code. You are adding them in a different order. Obviously, if you don;t fix the problem then the problem won't be fixed.

    Also, you should be calling AddWithValue rather than Add.

    Follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data. One of the examples there uses ExecuteNonQuery. Follow that example.
    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
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Save boolean value to MS Database

    thank you @jmcilhinney
    already successful now

Posting Permissions

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



Click Here to Expand Forum to Full Width