CheckBox! Specified cast is not valid

i had this error: specified cast is not valid when i try to add a new row to my grid that contain a itemtemplate checkbox as shown below. Whenever i use the word "Checked" instead of "Text", the error is shown. But what i want to do is to show the checkbox "checked" when my "choiceQn" is true and not show the "true" next to my checkbox. Please help me out if you can solve my problem.

ASP.NET

 <ItemTemplate>
     <asp:CheckBox ID="ChoiceCheckBox" runat="server" **Checked**='<%# Bind("ChoiceQn") %>'/>
 </ItemTemplate>

C#

    private void AddNewRowToGrid()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)UpdateQuestionGrid.Rows[rowIndex].Cells[0].FindControl("QuestionsTbx");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["QuestionId"] = i + 1;
                    drCurrentRow["Question"] = "";
                    drCurrentRow["ChoiceQn"] = false;
                    rowIndex++;
                }

                //add new row to DataTable
                dtCurrentTable.Rows.Add(drCurrentRow);

                //Store the current data to ViewState
                ViewState["CurrentTable"] = dtCurrentTable;

                //Rebind the Grid with the current data
                UpdateQuestionGrid.DataSource = dtCurrentTable;
                UpdateQuestionGrid.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        //SetPreviousData();
    }

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
   ASP.asc_questionupdate_aspx.__DataBinding__control15(Object sender, EventArgs e) in f:\ASC_FeedbackSystem(latest)\ASC_FeedbackSystem\asc\questionupdate.aspx:26
   System.Web.UI.Control.OnDataBinding(EventArgs e) +99
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +155
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2417
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   System.Web.UI.WebControls.GridView.DataBind() +4
   ASC_FeedbackSystem.questionupdate.AddNewRowToGrid() in F:\ASC_FeedbackSystem(latest)\ASC_FeedbackSystem\asc\questionupdate.aspx.cs:108
   ASC_FeedbackSystem.questionupdate.ButtonAdd_Click(Object sender, EventArgs e) in F:\ASC_FeedbackSystem(latest)\ASC_FeedbackSystem\asc\questionupdate.aspx.cs:161
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

As you're using nvarchar(1), try using Eval() for the Checked property of your CheckBox:

<ItemTemplate>  
    <asp:CheckBox ID="ChoiceCheckBox" runat="server" Checked='<%# Eval("ChoiceQn").ToString().Equals("1") %>'/>
</ItemTemplate>

Or consider using a boolean data type that's compatible with '<%# Bind("ChoiceQn") %>'/

Specified cast is not valid, when using a checkbox in either a details , Hi all I have a problem with a checkbox controls inside a Form View & a Detail View that look like this < td > Credentials : </ td > < td > < asp  i had this error: specified cast is not valid when i try to add a new row to my grid that contain a itemtemplate checkbox as shown below. Whenever i use the word "Checked" instead of "Text", the er

<asp:TemplateField HeaderText="Accident Benefit">
                    <ItemTemplate>
                        <asp:CheckBox ID="lblcbaccident" runat="server" Checked='<%#Convert.ToBoolean(Eval("Accident")) %>' />
                    </ItemTemplate>
                </asp:TemplateField>

boolean data type that's compatible with Convert.ToBoolean()

Bind Checkbox in GridView: Specified cast is not valid Solutions , Find answers to Bind Checkbox in GridView: Specified cast is not valid from the expert community at Experts Exchange. Find answers to Bind Checkbox in GridView: Specified cast is not valid from the expert community at Experts Exchange

I cann't get what you want. If you are going to add row to GridView then you don't need your code. You should add row just to your data source and after that call DataBind().

System.InvalidCastException: Specified cast is not valid In gridview , Add data types to the columns, by default it is string as. Hide Copy Code. private void FillCustomerInGrid() { gvGroupList.DataSource  Join a community of over 2.6m developers to have your questions answered on Specified cast is not valid on checkbox in ajax grid of UI for ASP.NET AJAX Grid. New here? Start with our free trials.

This code works:

 <asp:TemplateField HeaderText="Activo">
            <ItemStyle Width="150px"></ItemStyle>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBoxItem" Checked='<%# DataBinder.Eval(Container.DataItem,"Activo").ToString().Equals("1") %>' Enabled="false" runat="server"></asp:CheckBox>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="CheckBoxEdit" Checked='<%# DataBinder.Eval(Container.DataItem,"Activo").ToString().Equals("1") %>' runat="server"></asp:CheckBox>
            </EditItemTemplate>

Checkbox = null, Specified cast is not valid., Bind("bln_removed")%>'></asp:CheckBox> </edititemtemplate> The above line gives an error (Specified cast is not valid.) if the field You get this due to the fact that hasValue is null and Bind() expression will try to assign this to Checked property. To bind the Checked attribute value for your checkbox instance you need to specify default value (true/false) for hasValue. Thus you should be able to avoid the specified cast is not valid error.

Checked='<%# (Boolean)Eval("Active")  %>'

Gridview CheckBox Error: Specified Cast is not Valid, <asp:CheckBox ID="Live" runat="server" Checked='< %# Bind("Live") %>' /> </​EditItemTemplate> Message="Specified cast is not valid." Generally " specified cast is not valid " exception will be raised only if the value of Active is not boolean.

Specified cast is not valid on checkbox in ajax grid in UI for ASP.NET , Join a community of over 2.6m developers to have your questions answered on Specified cast is not valid on checkbox in ajax grid of UI for 

Checkbox = null, Specified cast is not valid., The above line gives an error (Specified cast is not valid.) if the field bln_removed is null. Without changing the SQL select statement how can 

https://social.msdn.microsoft.com/Forums/windows/e, i had this error: specified cast is not valid when i try to add a new row to my grid that contain a itemtemplate checkbox as shown below.

Comments
  • I don't know, but I do wonder why you loop over the existing table, creating a new row (in memory) for every row in the table and doing nothing with those rows, except after the end when you do something with the last one. What are you trying to achieve there?
  • geekswithblogs.net/dotNETvinz/archive/2009/06/04/…
  • i just replaced the textboxes with checkbox, but i'd face error when i tried to add a new row with "Checked='<%# Bind("ChoiceQn")%>"
  • What data type is ChoiceQn and are you using SQL or MySQL?
  • Hey its nvarchar(1), but in the class i take it as boolean.
  • But what if the programmer wants to check OldValues and NewValues? If you don't use Bind() then you lose that functionality.
  • Don't only post code, include an explanation how it addresses the problem. Also consider that you are posting this answer to a 6+ years old question that already had several answers, one of them even accepted. Consider focussing on answer newer questions, or questions that don't yet have an accepted answer.