0

When I debug after clicking the Update Button on the browser, it throws a NullReferenceException on the below line in GridView1_RowUpdating:

dr["Phone Number"] = e.NewValues["Phone Number"];

Full code:

public partial class DataBound : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) { }

    private void getCacheData()
    {
        if (Cache["DATASET"] != null)
        {
            GridView1.DataSource = Cache["DATASET"];
            GridView1.DataBind();
        }
    }

    private void getDataDB()
    {
        String connectionString = ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString;
        MySqlConnection con = new MySqlConnection(connectionString);
        MySqlDataAdapter adp = new MySqlDataAdapter("select `Customer ID`,`Customer Name`,`Phone Number` from customer", con);
        DataSet ds = new DataSet();
        adp.Fill(ds, "Customer");
        ds.Tables["Customer"].PrimaryKey = new DataColumn[] { ds.Tables["Customer"].Columns["Customer ID"] };
        Cache.Insert("DATASET", ds, null, DateTime.UtcNow.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        getCacheData();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            DataSet ds = (DataSet)Cache["DATASET"];
            DataRow dr = ds.Tables["Customer"].Rows.Find(e.Keys["Customer ID"]);
            dr["Phone Number"] = e.NewValues["Phone Number"];
            dr["Customer Name"] = e.NewValues["Customer Name"];
            Cache.Insert("DATASET", ds, null, DateTime.UtcNow.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration);
            GridView1.EditIndex = -1;
            getCacheData();
        }
        catch (Exception ex)
        {
            String errorMessage = ex.Message;
        }
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        getCacheData();
    }

    protected void btnGetDataDB_Click(object sender, EventArgs e)
    {
        getDataDB();
    }
}
Bhuwan Pandey
  • 477
  • 6
  • 19
  • Yes I know that dr is null, I debugged the code..but don't know why its coming null when I'm writing like this DataRow dr = ds.Tables["Customer"].Rows.Find(e.Keys["Customer ID"]); – Bhuwan Pandey Apr 15 '15 at 14:30
  • Yes checked that too, but I've "Customer ID" as my Primary key, don't know why its null here inside GridView1_RowUpdating ds.Tables["Customer"].PrimaryKey = new DataColumn[] { ds.Tables["Customer"].Columns["Customer ID"] }; – Bhuwan Pandey Apr 15 '15 at 14:35
  • Tried DataRow dr = ds.Tables["Customer"].Rows.Cast().FirstOrDefault(x => Convert.ToInt32(x["Customer ID"]).Equals(e.Keys["Customer ID"])); this line is not the problem....actual problem is e.Keys("Customer ID") is coming out null....I've checked 100 times...Key name is correct but no idea why it is not able to find the key name – Bhuwan Pandey Apr 15 '15 at 14:50
  • e.Keys["Customer ID"] returns a null value...IF I check ds.Tables["Customer"].PrimaryKey = new DataColumn[] { ds.Tables["Customer"].Columns["Customer ID"] }; it actually marks the "Customer ID" as primary key but returnning null when I write e.Keys["Customer ID"] – Bhuwan Pandey Apr 15 '15 at 15:03
  • When I debug Code Line : ds.Tables["Customer"].PrimaryKey = new DataColumn[] { ds.Tables["Customer"].Columns["Customer ID"] }; has value Value: {System.Data.DataColumn[1]}.....that means DataColumn1 that is "Customer ID" is being set as Primary Key.let me check this more ...Thank You – Bhuwan Pandey Apr 15 '15 at 15:49

0 Answers0