1
command = "INSERT INTO clients VALUES(" + NameTB.Text +")";
objCtx.SaveChanges();

List<Client> clients = new List<Client>();
clients = objCtx.ExecuteStoreQuery<Client>("Select * from clients").ToList();

string x = "";
for (int i = 0; i < clients.Count; i++)
     x += clients[i].Name.ToString();//exception here

MessageBox.Show(x);

I just start to work with EF and have no idea how to fix this; I took the code from here

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43

1 Answers1

2

Sounds like the Name property is null in the Client data that is returned.

Either have a constraint in the database that this field is required or add a null check:

string x = "";
for (int i = 0; i < clients.Count; i++) {
     if (!String.IsNullOrEmpty(clients[i].Name)) {
          x += clients[i].Name.ToString();
     }
}

Also, if you're going to work with many clients and your string is going to get big I suggest you take a look at StringBuilder.

Omri Aharon
  • 16,959
  • 5
  • 40
  • 58