A null value in the data will translate to System.DBNull.Value. You will have to take this into account in your DataBinding expression. Alternatively, you can set initial value in the empty record you create. Which is easiest to do depends on the exact requirements.
I take it the database field allows null values. In that case, the UI should take into account possible null values even for existing records.
If null values are not logical, you may want to disallow null values in the database field, and give it an initial value. Creating a new record should then set the value properly, and solve the UI problem.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Hi Krishna, if you have in fact defined your boolean field to have a default value of 'false' then you should have no issue. My advice is to change your insert stored procedure to not accept (i.e. not look for) an input parameter for your boolean field.
// Create a new DataRow
DataRow dr = dt.NewRow();
// Add the new DataRow to the DataTable
dt.Rows.InsertAt(dr, 0);
to
// Create a new DataRow
DataRow dr = dt.NewRow();
// Set default value for status
dr["[Status]"] = false;
// Add the new DataRow to the DataTable
dt.Rows.InsertAt(dr, 0);
A new DataRow has System.DBNull.Value for every field value. If the UI can't process that, you can replace it with a real value like I show here.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
krishna.bb
Member
129 Points
112 Posts
CheckBox column in GridView
Aug 14, 2012 06:02 PM|LINK
Hello Friends,
I need your help :)
Here is my requirement:
I have a check box column in my GridView.
Little background info:
I am using C#, VS 2008, .NET 3.5 and SQL Server 2008 R2.
Column name is Status and DataType is Boolean.
Three things I am doing here:
The column is a TemplateField,
Here is the code:
<asp:TemplateField HeaderText="Status"> <EditItemTemplate> <asp:CheckBox ID="cbStatusW" runat="server" Checked='<%# (Convert.ToBoolean(Eval("[Status]"))) %>' /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="cbStatusR" runat="server" Enabled="false" Checked='<%# (Convert.ToBoolean(Eval("[Status]","{0}"))) %>'/> </ItemTemplate> </asp:TemplateField>This code works when I:
1) Edit the record, it works fine
if the value in the DB is 1, it shows the check box value as "checked" and I am able to change the value and update the record.
2) See the column, it shows the correct value
if the value in the DB is 0, it shows the check box value as "unchecked".
3) When I add a new record, it fails:
I am actually, created a new empty record and it is failing because new record does not have a value for the checkbox (eg: true or false)
The error message: System.InvalidCastException was unhandled by user code. Object cannot be cast from DBNull to other types.
Please help me out :)
Thank you!
Krishna
erdeepak.sin...
Member
32 Points
10 Posts
Re: CheckBox column in GridView
Aug 14, 2012 06:27 PM|LINK
jst check for values on gridview binding time
onDataBound event if it is not a true or false value handle it by setting false
clevesteve
Participant
1405 Points
406 Posts
Re: CheckBox column in GridView
Aug 14, 2012 08:49 PM|LINK
are you using the InsertItem (NewItem?) template? Or another method of inserting data?
Is the insert done via stored procedure? try setting
SqlDataSourceX.InsertParameters("Status").DefaultValue = Falseor if you need it default = true, set it equal to true. If you are generating the box before inserting the line, you can set
SqlDataSourceX.InsertParameters("Status").DefaultValue = CheckBoxID.SelectedAlternatively, you can set a default value in your DB table definition to "true" or "false" if a new entry should always be set to one or another.
superguppie
All-Star
48225 Points
8679 Posts
Re: CheckBox column in GridView
Aug 15, 2012 07:14 AM|LINK
A null value in the data will translate to System.DBNull.Value. You will have to take this into account in your DataBinding expression. Alternatively, you can set initial value in the empty record you create. Which is easiest to do depends on the exact requirements.
I take it the database field allows null values. In that case, the UI should take into account possible null values even for existing records.
If null values are not logical, you may want to disallow null values in the database field, and give it an initial value. Creating a new record should then set the value properly, and solve the UI problem.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
krishna.bb
Member
129 Points
112 Posts
Re: CheckBox column in GridView
Aug 15, 2012 02:45 PM|LINK
Thank you superguppie for your response.
The database does not allow null values, but when I create a new blank row, by default it should selected false.
I have a Add link button outside my GridView, when I click on the Add button, it will add an empty row to the GridView control.
Please see the below code.
// Create a DataSet
DataSet dsEmpInfo = new DataSet();
// Fill the DataSet
dsEmpInfo = GetEmpInfo(ddlEmpNo);
// Get the DataTable
DataTable dt = dsEmpInfo .Tables[0];
// Create a new DataRow
DataRow dr = dt.NewRow();
// Add the new DataRow to the DataTable
dt.Rows.InsertAt(dr, 0);
// Make the first row of the Grid View editable
gvAR.EditIndex = 0;
// Set the Data Soruce for the Grid View
gvAR.DataSource = dt;
// Bind the Data
gvAR.DataBind();
// Change the "Update" Link button name to "Insert"
((LinkButton)gvAR.Rows[0].Cells[0].FindControl("lbUpdate")).Text = "Insert";
Finally, when I click the Insert(update) link button in the GridView, the record will be saved to the DataBase.
Krishna
clevesteve
Participant
1405 Points
406 Posts
Re: CheckBox column in GridView
Aug 15, 2012 03:21 PM|LINK
Hi Krishna, if you have in fact defined your boolean field to have a default value of 'false' then you should have no issue. My advice is to change your insert stored procedure to not accept (i.e. not look for) an input parameter for your boolean field.
superguppie
All-Star
48225 Points
8679 Posts
Re: CheckBox column in GridView
Aug 16, 2012 09:13 AM|LINK
I think I would change
to
A new DataRow has System.DBNull.Value for every field value. If the UI can't process that, you can replace it with a real value like I show here.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
siddartha
Member
2 Points
1 Post
Re: CheckBox column in GridView
Dec 04, 2012 03:35 AM|LINK
hi i m working on
almost same project
canu help me insome ofthe issues
when ever i select a checkbox it should call a function in which i have to call a sproc
m using jsim.dll and iframe styles in that there are two options checkedmethodname and uncheckedmethodname