This is selection changed event :
private void cbUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedUser = (sender as ComboBox).SelectedItem.ToString();
GetUserInformation();
}
GetUserInformation is just selecting password from database. Users are deleted from the database and then the following refreshes the ComboBox items:
public void FillComboBox()
{
cbUsers.ItemsSource = null;
HProDataContext db = new HProDataContext();
var _UserName = (from d in db.users select d.username).ToList();
cbUsers.ItemsSource = _UserName;
}
HProDataContext db = new HProDataContext();
var _UserID = (from d in db.users where d.username == cbUsers.Text select d.id).SingleOrDefault();
user u = db.users.Single(p => p.id == _UserID);
db.users.DeleteOnSubmit(u);
db.SubmitChanges();
cbUsers.ItemsSource = null;
cbUsers.Text = null;
FillComboBox();
When using this last method it gives such error:
Object reference not set to an instance of an object.
The error falls on this line of the FillComboBox method:
SelectedUser = (sender as ComboBox).SelectedItem.ToString();
Does anyone have an idea as to what is wrong?
I’d guess that
SelectedItemisnulland therefore you’re callingToStringon nothing.Consider trying this:
However, doesn’t your
ComboBoxhave an identifier? This can allow you to refrain from unnecessary conversions withas: