Checkbox in Datagrid failing on Add New Row
-
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!
-
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!
-
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!
-
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!
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