Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Checkbox in Datagrid failing on Add New Row

Checkbox in Datagrid failing on Add New Row

Scheduled Pinned Locked Moved ASP.NET
databasehelpdata-structuresdebuggingannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BORN again
    wrote on last edited by
    #1

    Hello & Smiles, I had to add a new field to one of my old projects which was designed using Datagrid, it is a checkbox field with datatype as 'Bit'(SQL 2005)same as boolean. I added the checkbox in datagrid as below: In the Insert and Update Sub's I added the code as below: Dim chkbox As CheckBox = CType(e.Item.Cells(9).FindControl("cbcurrentdriver"), CheckBox) If chkbox.Checked = True Then Command.Parameters.Add("@currentdriver", SqlDbType.Bit, 2).Value = "True" Else Command.Parameters.Add("@currentdriver", SqlDbType.Bit, 2).Value = "False" End If Also, included the field in Databind for Datagrid with select * from etc... This works fine when I open the page in update mode and shows the status of checkbox as checked or unchecked depending upon it's value in database. My datagrid has a feature 'Add New row' which blank row and this part fails giving me the error - Conversion from type 'DBNull' to type 'Boolean' is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid. Source Error: Line 1175: Line 1176: Line 1177: Line 1178: I have been struggling with Null checks for checkboxes etc... for last two days. Any help is Gratifying. Thankyou. Aartee. ...HE is watching Us All!

    K 1 Reply Last reply
    0
    • B BORN again

      Hello & Smiles, I had to add a new field to one of my old projects which was designed using Datagrid, it is a checkbox field with datatype as 'Bit'(SQL 2005)same as boolean. I added the checkbox in datagrid as below: In the Insert and Update Sub's I added the code as below: Dim chkbox As CheckBox = CType(e.Item.Cells(9).FindControl("cbcurrentdriver"), CheckBox) If chkbox.Checked = True Then Command.Parameters.Add("@currentdriver", SqlDbType.Bit, 2).Value = "True" Else Command.Parameters.Add("@currentdriver", SqlDbType.Bit, 2).Value = "False" End If Also, included the field in Databind for Datagrid with select * from etc... This works fine when I open the page in update mode and shows the status of checkbox as checked or unchecked depending upon it's value in database. My datagrid has a feature 'Add New row' which blank row and this part fails giving me the error - Conversion from type 'DBNull' to type 'Boolean' is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid. Source Error: Line 1175: Line 1176: Line 1177: Line 1178: I have been struggling with Null checks for checkboxes etc... for last two days. Any help is Gratifying. Thankyou. Aartee. ...HE is watching Us All!

      K Offline
      K Offline
      kinnuP
      wrote on last edited by
      #2

      hi , try to use default value in database as 'F'.it may help u thanks and regards, Kinnu

      B 1 Reply Last reply
      0
      • K kinnuP

        hi , try to use default value in database as 'F'.it may help u thanks and regards, Kinnu

        B Offline
        B Offline
        BORN again
        wrote on last edited by
        #3

        Hello Kinnu, Thankyou for your reply. I did try giving it a default value 'F' with 7 without quotes, I even tried default value false and NULL but it didn't work and SQL database is giving me an error when I type the default value as: Error validating the default value for column 'currentdriver'. It does not give me this error when I enter default value as NULL or 0 but my program is still not working and giving me the same conversion (DBNULL to Boolean) error. Thanks once again. Please let me know if you have any other tips or suggestions.

        Aartee. ...HE is watching Us All!

        M 1 Reply Last reply
        0
        • B BORN again

          Hello Kinnu, Thankyou for your reply. I did try giving it a default value 'F' with 7 without quotes, I even tried default value false and NULL but it didn't work and SQL database is giving me an error when I type the default value as: Error validating the default value for column 'currentdriver'. It does not give me this error when I enter default value as NULL or 0 but my program is still not working and giving me the same conversion (DBNULL to Boolean) error. Thanks once again. Please let me know if you have any other tips or suggestions.

          Aartee. ...HE is watching Us All!

          M Offline
          M Offline
          Marcus J Smith
          wrote on last edited by
          #4

          Since this is a bit column you need the default value to be 0 which is false and then you would avoid this. Also if you dont want a default value, create a Protected Function in the code behind and pass the value from the Datagrid DB value and accept it as an Object, check for DBNull and if it is return a 0/False whatever works. Cleako

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups