Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

linq get value from Id

now i have this from sql database:

CourseId:    CousreCode
1            0507       
4            0508
5            0509
6            0511
7            0512
8            0510
9            0515

so after i clicked the data cell it will show the CourseId in the textbox but now i want to get the course code value that it is pair with the CourseId

here’s what i want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

so after i click a data cell it will show the CourseId in the text box
but since CourseCode is in another table i dont know how to show the CourseCode value to the text box

here’s what i’m stuck at

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            using (AP2Context context = new AP2Context())
            {
                tbCourseId.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                tbCourseCode.Text = ...
            }
        }

>Solution :

Using your Courses DbSet, you can maybe write:

tbCourseCode.Text = context.Courses.Find(...).CourseCode;

or (to reload from Database)

tbCourseCode.Text = context.Courses.First(c => c.CourseId == ...).CourseCode;

Replace ... with the selected course CourseId with the right type:

  • If string, reuse the value you got from the expression you write for tbCourseId.Text.
  • If int, convert it to int using int.Parse() if necessary for example.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading