0

First My English it so bad, i hope you know what i want to ask you.

This is my code

private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string MaNV = dataGrid.SelectedValue.ToString();
    Nhan_Vien nv = dsnhanvien.Single(a => a.MaNV.Trim() == MaNV);
    txtMaNV.Text = nv.MaNV;
    txtHo.Text = nv.Ho;
    txtTen.Text = nv.Ten_Dem;
    txtDiaChi.Text = nv.Dia_Chi;
    txtGioiTinh.Text = nv.Gioi_Tinh;
    txtLuong.Text = nv.Luong.ToString();
}

I get an error when i click 1 colum in my DataGrid

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Core.dll

Additional information:

Value cannot be null

stuartd
  • 62,136
  • 13
  • 120
  • 150
  • 3
    On which line is the error? Please learn how to debug your code. – DavidG Dec 23 '16 at 13:35
  • Set a break-point and run it in debug mode. What is the `MaNV` value? – McNets Dec 23 '16 at 13:36
  • this line Nhan_Vien nv = dsnhanvien.Single(a => a.MaNV.Trim() == MaNV); – Nhân Hirokira Dec 23 '16 at 13:36
  • MaNV it is String – Nhân Hirokira Dec 23 '16 at 13:37
  • Of course is a string, but it get `dataGrid.SelectedValue.ToString()` that I suppose is `null`. – McNets Dec 23 '16 at 13:38
  • Just replace dataGrid.SelectedValue.ToString(); line with e.SelectedValue.ToString(); – Anurag_Soni Dec 23 '16 at 13:39
  • //dataGrid.SelectedValue.ToString(); line with e.SelectedValue.ToString();// It is not Working :( How i can fix this mcNets :( Help me :( – Nhân Hirokira Dec 23 '16 at 13:42
  • Sorry for the mistaken close. – stuartd Dec 23 '16 at 13:49
  • @David I just had the comment notification? – stuartd Dec 23 '16 at 13:52
  • @stuartd Cool, wasn't sure if hammerers get pinged or not. – DavidG Dec 23 '16 at 13:52
  • Do you have a stack trace of exception? – tym32167 Dec 23 '16 at 13:52
  • If MaNV is null in `a.MaNV.Trim()` , Trim will be called on null and can cause the exception. You could try using `a.MaNV?.Trim()` instead – Me.Name Dec 23 '16 at 13:59
  • Use the debugger to solve problems like this. It will tell you the line that was responsible for throwing the exception, and you can inspect the objects used in that line to see which one is null. – Cody Gray Dec 23 '16 at 14:00
  • @Cody I Know a line but i dont know why MaNV get Null when i chose 1 information in my dataGrid – Nhân Hirokira Dec 23 '16 at 14:08
  • If any `Nhan_Vien` inside `dsnhanvien` has a `MaNV` that is null, the `Single` can throw the exception (on `Trim`). Did you try using `a.MaNV?.Trim()` ? (The extra `?` is to create a null-conditional operator) – Me.Name Dec 23 '16 at 14:23
  • Right ? dsnhanvien.Single(a => a.MaNV?.Trim() == MaNV); but i get erordoes not contain a definition for 'Single' and the best extension method OverLoad – Nhân Hirokira Dec 23 '16 at 14:38
  • With the line unchanged from the original (except the `?`) ? `Nhan_Vien nv = dsnhanvien.Single(a => a.MaNV?.Trim() == MaNV);` If the code worked before, `Single` should still be valid. As long as `dsnhanvien` is a valid enumerable source and you have a `using System.Linq`. – Me.Name Dec 23 '16 at 15:26
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – rene Dec 23 '16 at 22:11

0 Answers0