Reputation: 47
I am making a basic patient record system on visual studio (windows form application, c#).
When a user tries to insert the same firstname and surname, the application should give an error like:
You can't insert the same name twice.
I just don't know how to get data directly from sql on visual . Can anyone help ?
Upvotes: 0
Views: 79
Reputation: 2169
I hope this code helps. I have assumed that you are using Entity Framework
var newPatient = ...;
if
(
Context.Patients.Count
(
x=>
x.Name==newPatient.Name &&
x.Family==newPatient.Family
) > 0
)
MessageBox.Show("This is an existing patient");
Edit (Based on your comment):
var newPatient= new Patient();
newPatient.Name = textBox1.Text;
newPatient.Family = textBox2.Text;
if
(
Ort.Grid.Count
(
x=>
x.Name==newPatient.Name &&
x.Family==newPatient.Family
) > 0
)
MessageBox.Show("This is an existing patient");
Upvotes: 0
Reputation: 347
Make Firstname and lastname as primerykey combine in your db table, that will not allow duplicate... but making names as primary key is not good practice because may two patients have same first and last names
Upvotes: 1