Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me how to solve this problem "Object reference not set to an instance of an object."

I have a datagrid & above error is generated in following code
protected void Bill_ItemCommand(object source, DataGridCommandEventArgs e)
    {
       // CheckBox ck = new CheckBox();
        CheckBox ck = (CheckBox)e.Item.FindControl("chk");
        string upval;
        if (ck.Checked)
            upval = "1";
        else
            upval = "0";

        if (e.CommandName == "Update")
        {
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/") + "Bills_db.mdb;Persist Security Info=False");


            // string strSql = @"Insert into January (Due_Date) values (txtDue_Date.txt)";

            string strSql = "update  tblBills set Remarks=" + upval + " where ID=" + (Bill.DataKeys[e.Item.ItemIndex]);
            con.Open();
            OleDbCommand cmd = new OleDbCommand(strSql, con);
            // cmd.Parameters.AddWithValue("@Name", name);



            cmd.ExecuteNonQuery();


            con.Close();
            BindData();

        }

I hav a check box in datagrid. When I view any page except the 1st page(I have 6 page in all) in datagrid that time it throws the above error. How to solve it? Please help me.
Posted
Updated 21-Mar-11 19:31pm
v2
Comments
JF2015 22-Mar-11 1:31am    
Added code formatting.
Sharma Richa 22-Mar-11 2:23am    
have you added datakeyfield to datalist

1 solution

See Here[^]. it is already posted in Q&A Section.

P.S: You are trying to do Null to Other type conversion. Check your code by debugging where your getting 'Null' Value.
 
Share this answer
 
Comments
Sandeep Mewara 22-Mar-11 7:28am    
Good answer Eswar. 5!
TweakBird 22-Mar-11 12:27pm    
Thank you Sandeep.

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