narasiman rao

narasiman rao

  • NA
  • 519
  • 766k

In grid view i am using checkbox

Dec 12 2012 2:24 AM

 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.

Answers (1)