Tuesday, 24 April 2012

Catch a NullReference Exception For Data Grid In Silver Light 4,C#

Error Code :


private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {             
                    studentClient stud = new studentClient();
                    stud.GridSelectCompleted += new EventHandler<GridSelectCompletedEventArgs>(stud_GridSelectCompleted);
                    stud.GridSelectAsync(Convert.ToInt32(((TextBlock)dataGrid1.Columns[0].GetCellContent(dataGrid1.SelectedItem)).Text));             
            }
            catch (Exception ex)
            {
                throw ex;
            }
        } 
Null Reference Exception In Silver Light Data Grid C#


Hi
This type Of Error I faced working with silverlight 4 ,C#

Null Reference Exception In Data Grid

" Value Can Not Be Null "
" Parameter Name : Data Item "

Use This Line To Solve This Error :

 if (dataGrid1.CurrentColumn != null && dataGrid1.CurrentColumn.DisplayIndex == 1)
                {

                }


Solved Code:

private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (dataGrid1.CurrentColumn != null && dataGrid1.CurrentColumn.DisplayIndex == 1)
                {
                    studentClient stud = new studentClient();
                    stud.GridSelectCompleted += new EventHandler<GridSelectCompletedEventArgs>(stud_GridSelectCompleted);
                    stud.GridSelectAsync(Convert.ToInt32(((TextBlock)dataGrid1.Columns[0].GetCellContent(dataGrid1.SelectedItem)).Text));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        void stud_GridSelectCompleted(object sender, GridSelectCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                txtid.Text = Convert.ToString(e.Result.id);
                txtname.Text = e.Result.name;
                txtage.Text = Convert.ToString(e.Result.age);
            }
            //throw new NotImplementedException();
        }

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
^ Scroll to Top Twitter Bird Gadget