In previous article we have already learn about bind label or text box using ado.net. Today, i will discuss about edmx file. If you want to bind label or text box in windows form, edmx file is the best solution for this type of problem. There are following steps to bind the label or text box:
I am giving you a whole code video for user friendly:
- First to add some label and text boxes in the form Like:
- Now, add edmx file in the solution
- Now, copy this code and paste in the button click event handler block.
private void button1_Click(object sender, EventArgs e)
int id = Convert.ToInt32(idtxt.Text);
Student_DBEntities dc = new Student_DBEntities();
var getrecord = dc.Student_record.Where(a => a.Id == id).SingleOrDefault();
Studentidtxt.Text = getrecord.Student_Id;
studentnametxt.Text = getrecord.Student_Name;
fathernametxt.Text = getrecord.Father_Name;
MotherNametxt.Text = getrecord.Mother_Name;
Addresstxt.Text = getrecord.Address;
- Student_DBEntities is a data context name, Create a object of that class, Access public property(Student_record -Screen Shot mentioned in the link) by using instance name.
- Access single record with the help of where clause with lambda expression also use SingleOrDefault( ) method.
- By using getrecord variable you can access table fields or you can say model class fields.
- Get each fields which is required by you, returning result assign in Text boxes.