Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Object reference not set to an instance of an object - NullReferenceException

$
0
0

Hi Everyone,

I have a radListView that display a list of users with a few textboxes at the side, once the user select a user it be below function is called to populate the textboxes with data from a LinqToSQL query. This was working great and for some strange reason, I start getting an error message "Object reference not set to an instance of an object" which is coming from the NullReferenceExpection. If I was to remove the message from the NullReferenceExpection block of code, the function/form works correctly.  Can some please advice how I can correct my coding?

 private void DisplayUserInfo()
        {
            using (NorthWindDataContext db = new NorthWindDataContext())
                try
                {
                    var userinfo = (from u in db.apps_Users
                                   where u.Username == radListViewUsers.SelectedItem.Value.ToString()
                                   select new { fName = u.FirstName, lName = u.LastName, email = u.EmailAddress,  expired = u.ExpiredDate, pass = u.Password, gname = u.apps_Role.UserRole, active = u.IsActive }).FirstOrDefault();
                    txtFirstName.Text = userinfo.fName;
                    txtLastName.Text = userinfo.lName;
                    txtEmail.Text = userinfo.email;
                    txtPassword.Text = userinfo.pass;
                    ddlRole.Text = userinfo.gname;
                    txtExpiredDate.Value = userinfo.expired;
                    radChkBoxActive.Checked = userinfo.active;
                    lblActive.Text = radChkBoxActive.Checked == true ? "User account is now enable." : "User account is now disable.";
                }
                catch(NullReferenceException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

Thank you.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>