Results 1 to 4 of 4

Thread: Checkbox Inside Gridview

  1. #1
    Junior Member
    Join Date
    Jun 12
    Posts
    23

    Checkbox Inside Gridview

    I bind the Gridview with SQL. I have one bit data type column in SQL table.
    I want to have one checkbox type column in Gridview. My code is working if database have true or false in all rows. But if the row contains NULL, my code doesn't work. Help me in Modifying the code.

    SOURCE CODE : -
    Code:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
            <Columns >
            <asp:BoundField DataField="Question_ID" HeaderText="Ques ID :"  />
            
            <asp:TemplateField  >
            <ItemTemplate >
            <asp:CheckBox  ID="chkbox" Checked='<%#Convert.ToBoolean(Eval("Active")) %>' runat ="server"  />
            </ItemTemplate> 
            </asp:TemplateField> 
            
            </Columns>
            </asp:GridView>
    SERVER SIDE CODE : -
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                query = "Select * from INSERT_QUESTION";
                cmd = new SqlCommand(query, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    ERROR : -Object cannot be cast from DBNull to other types.

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,744

    Re: Checkbox Inside Gridview

    Hello,

    Couple things I would suggest here...

    1. Set a default value on the Active field in the database such that whenever a row is added, regardless of whether a value is provided, the Active field will always be given a true or a false, depending on your requirements.
    2. What is the data type of the Active field? Why do you need to convert it in the first place?
    3. Don't do the conversion logic in the ASPX markup, this is the wrong place to do it. Doing it this way means that you are closely coupling the UI to the Business Logic, which is never a good place to be in
    4. Try to understand the error message that you are getting back. Let me try to explain...

    The Convert.ToBoolean method has a number of overrides, meaning that it can accept a number of different types, and it will try to figure out what the boolean equivalent is:

    http://msdn.microsoft.com/en-us/libr...toboolean.aspx

    You will notice from the above list, that a DBNull value is not mentioned. The Convert.ToBoolean method doesn't know how to convert from a DBNull to a boolean, and that is what you are giving it. In order to correct this, you are going to need to first check whether or not the value that you are getting is a DBNull, if it is, then take appropriate action, i.e. default it to false, or to true. If it isn't, carry on with the conversion.

    Hope that helps!

    Gary

  3. #3
    Hyperactive Member
    Join Date
    Apr 08
    Posts
    456

    Re: Checkbox Inside Gridview

    ITS DONE!!!!!
    You can handle the NULL value in Query itself and can return 0 or false from there for example
    Code:
    Select isNull(ColName,'False') as [Active] from INSERT_QUESTION

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,744

    Re: Checkbox Inside Gridview

    Hello,

    Yes, that is certainly an option.

    I would still suggest that correct handling of the data within your code is something that you need to look into.

    Thanks

    Gary

Posting Permissions

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