shalsoft
shalsoft

Reputation: 457

can't trace e.NewValues at RowUpdating event of grid view

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.....

Upvotes: 0

Views: 780

Answers (1)

Ali
Ali

Reputation: 2592

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.

Upvotes: 1

Related Questions