'Display only one Teacher Id on Teachers Column in DatagridView
I am doing a application for a school using C# and database with SQL Server Management Studio and in one of the forms I want that when the teacher logged in only view the records from his, in order to hide the records from another teachers, so in the part of the code when I display all the teachers in datagridview I want to filter the "ProfessorId", column teacher id referent, to the id of the teacher that logged in while the application is running.
I am using my table for show classes from each teacher (ProfessoresTurmas), the column contain:
- Id Primary Key
- ProfessorId(Teacher's Id) Foreign Key from Teachers Table
- CursoId(Teacher's Course) Foreign Key from Courses Table
- TurmaId(Teacher's Class) Foreign Key from Classes Table
Image of the records of the table
Query working in sql server management studio
Thats the code that worked but only with the id that I wrote in the code, "Pr1" is one of the ids from the table but I want to detect automatically what's the teacher's id that is logged in:
private void DisplayTeachers()
{
Con.Open();
string Query = "Select * from ProfessorTurmas where ProfessorId = 'Pr1'";
SqlDataAdapter sda = new SqlDataAdapter(Query, Con);
SqlCommandBuilder Builder = new SqlCommandBuilder(sda);
var ds = new DataSet();
sda.Fill(ds);
DataGridView_student.DataSource = ds.Tables[0];
Con.Close();
}
Thats the code that I tried to wrote instead of 'Pr1' with UserCache.IdUser, thats the teacher's id logged in while the application is running:
private void DisplayTeachers()
{
Con.Open();
string Query = "Select * from ProfessorTurmas where ProfessorId = " + UserCache.IdUser;
SqlDataAdapter sda = new SqlDataAdapter(Query, Con);
SqlCommandBuilder Builder = new SqlCommandBuilder(sda);
var ds = new DataSet();
sda.Fill(ds);
DataGridView_student.DataSource = ds.Tables[0];
Con.Close();
}
But shows me this error:
System.Data.SqlClient.SqlException: 'Invalid column name 'Pr1'.'
In this line:
sda.Fill(ds);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|