Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
i am doing the application using grid view.in the database for check box i am using bit datatype.

for the check box in the database field as follows;

Columnname datatype length allownulls
Activate    bit  1   1

in the source for the grid view code as follows;

 <asp:TemplateField  HeaderText=Activate SortExpression="Activate">
                  <itemtemplate>
                       <asp:CheckBox ID="chkStatus" runat="server"
                           AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                         Checked='<%# Convert.ToBoolean(Eval("Activate")) %>'
                            Text='<%# Eval("Activate").ToString().Equals("True") ? " Activate " : " Not Activate " %>' />
                  </itemtemplate>
In the aspx page for check box code as follows;
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
        string vid = row.Cells[1].Text;
        bool status = chkStatus.Checked;
        string query = "UPDATE Vendors SET Activate = @Activate WHERE VendorId = @VendorId";
        SqlConnection conn = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
        SqlCommand com = new SqlCommand(query, conn);
        com.Parameters.Add("@Activate", SqlDbType.Bit).Value = status;
        com.Parameters.Add("@VendorId", SqlDbType.Bit).Value = vid;
        conn.Open();
        com.ExecuteNonQuery();
        conn.Close();
    }

when i run the grid view it shows the error as follows

Object cannot be cast from DBNull to other types.

from my coding in the aspx and source page what is error please help me.
Posted

I feel this is the problem of null value i.e. value not selected for column "Activate" in database. when you are binding the grid view directly with the dataset/datatable it contains DBNULL value in this column.

Instead of directly binding to the table column. You may create a property/method. it's responsibility shall be to check if the value coming from database is DBNULL. Here you may take decision whether you want to show true/false in case of null values.

In all, this error is cause of you are trying to convert "Activate" column's value to boolean. Which could be Null. Null could not be converted to boolean.
 
Share this answer
 
Hi,
I think the problem is with the Activate column type. Change the allowNull column to 0 and try you will succeed.

Thanks
 
Share this answer
 
In case you will change the allownull flag, that means the value must be present in Activate column. That could be either true or false based on your business logic. Yes, this would resolve this problem. However, you would not be able to identify whether Activate column has response or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900