0

i have problem with my grid view. i have this code for grid view binding :

public void FillGrid1(string StartAlpha1, string ColumnName1, string SearchText1)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var query = Enumerable.Repeat(new
                {
                    Id = default(int),
                    ReasonText = string.Empty
                }, 0).ToList();
            if (StartAlpha1 == "All")
            {
                query = db.Reasons.Select(r => new { Id = r.Id, r.ReasonText }).FilterForColumn(ColumnName1, SearchText1).ToList();
            }
            else
            {
                query = db.Reasons.Select(r => new { Id = r.Id, r.ReasonText }).FilterForColumn(ColumnName1, SearchText1).Where(x => x.ReasonText.StartsWith(StartAlpha1)).ToList();
            }
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("ReasonText", typeof(string)));
            foreach (var item in query)
            {
                if (item != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["Id"] = int.Parse(item.Id.ToString());
                    dr["ReasonText"] = item.ReasonText.ToString();
                    dt.Rows.Add(dr);
                }
            }
            myDataSet.Tables.Add(dt);
            lbl_count_reason.Text = myDataSet.Tables[0].Rows.Count.ToString();
            if (myDataSet.Tables[0].Rows.Count > 0)
            {
                DataView myDataView = new DataView();
                myDataView = myDataSet.Tables[0].DefaultView;
                if (this.ViewState["SortExp1"] != null)
                {
                    myDataView.Sort = this.ViewState["SortExp1"].ToString()
                             + " " + this.ViewState["SortOrder1"].ToString();
                }
                GV_ViewReasons.DataSource = myDataView;
                GV_ViewReasons.DataBind();
            }
            else
            {
                myDataSet.Tables[0].Rows.Add(myDataSet.Tables[0].NewRow());
                GV_ViewReasons.DataSource = myDataSet;
                GV_ViewReasons.DataBind();
                int columncount = GV_ViewReasons.Rows[0].Cells.Count;
                GV_ViewReasons.Rows[0].Cells.Clear();
                GV_ViewReasons.Rows[0].Cells.Add(new TableCell());
                GV_ViewReasons.Rows[0].Cells[0].ColumnSpan = columncount;
                GV_ViewReasons.Rows[0].Cells[0].Text = "No Records Found";
            }
            if (GV_ViewReasons.Rows.Count != 0)
            {
                SetPageNumbers1();
            }
        }
    }

and this is for row updating event :

 protected void GV_ViewReasons_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int reasonid = Convert.ToInt32(GV_ViewReasons.DataKeys[e.RowIndex].Value.ToString());
        if (!string.IsNullOrEmpty(e.NewValues["ReasonText"].ToString()))
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                db.UpdateReason(reasonid, e.NewValues["ReasonText"].ToString().Trim());
                db.SubmitChanges();
                GV_ViewReasons.EditIndex = -1;
                this.FillGrid1((String)Session["StartAlpha1"] ?? null, (String)Session["ColumnName1"] ?? null, (String)Session["SearchText1"] ?? null);
                UpdatePanel18.Update();
                MPE1.Show();
            }
        }
    }

here if (!string.IsNullOrEmpty(e.NewValues["ReasonText"].ToString())) this line throws Object refrences not instance.... i can't getting new value with this key.

how ever my FillGrid1 have thid field. what i'm going wrong here....

please help me.....

shalsoft
  • 457
  • 1
  • 5
  • 13

1 Answers1

1

because you are casting the value of e.NewValues["ReasonText"] to string before you check whether the value is null, so you need to do this like below:

    if(e.NewValues["ReasonText"] != null && 
         !string.IsNullOrEmpty(e.NewValues["ReasonText"].ToString())) { //... }

this would check the null value of the e.NewValues["ReasonText"] first and if it was not null then it will go to the next condition.

Ali
  • 2,574
  • 1
  • 17
  • 24
  • i have another grid view on same page and i'm getting it's e.NewValue why this grid view not. which also have same data binding structure. – shalsoft Jun 09 '14 at 07:27
  • it is totally depends on the value of your `e.NewValues["ReasonText"]`, on the other gridview you might have NO null value in `e.NewValues["ReasonText"]`, but in this one you may have one. it is always good to check if the value is null before you cast it to any other data type though. – Ali Jun 09 '14 at 07:28
  • but i just want to got new values entered by user. this is the way to for just cheking. how ever at row updating grid view should have e.NewValue["ReasonText"] because it's key function of this event. – shalsoft Jun 09 '14 at 07:34
  • do you still get the `Object references not instance` on that line after the code above? – Ali Jun 09 '14 at 07:38
  • no but it's by pass my problem. means i don't getting new values entered from user. – shalsoft Jun 09 '14 at 07:41
  • 1
    Ok, this might help you in regards to the new issue: [check here](http://stackoverflow.com/questions/2429864/gridviews-newvalues-and-oldvalues-empty-in-the-onrowupdating-event) – Ali Jun 09 '14 at 08:00
  • ok,i try it but still not getting e.NewValues["..."] by this way. – shalsoft Jun 09 '14 at 08:30