Saber
Saber

Reputation: 27

SQL Parameter And int Value

I use a query in ado.net to read the information to be displayed in the datagrid, but the problem is that my SQL parameter, which is related to Teacher_Id, has a problem and does not allow the information to be displayed. What is your solution?

I remember, there is no error, just does not show information in datagrid!

I executed this code in SQL and it works properly but I do not know what is the problem with ado.net?

I used the same code before with the LIKE and it worked properly on ado.net

my code is:

public DataTable Read_For_Properties(string University_Name, int Teacher_Id, string Year, string Term, string Lesson_Code, string Houre, string ClassRoom_Name)
{
    SqlConnection SQL_Con = new SqlConnection(@"Data Source=SQLSERVER\SQLSERVER;Initial Catalog=DB1;Integrated Security=True");

    SqlCommand SQL_Com = new SqlCommand(@"select 
                                            Turns.Id,
                                            case Humen.Discriminator when 'Student' then Humen.Name end as 'Name',
                                            case Humen.Discriminator when 'Student' then Humen.Family end as 'Family',
                                            Turns.Score as 'Score'
                                            from Turns 
                                            inner join Lessons on Lessons.Id = Turns.Lesson_Id
                                            inner join Universities on Universities.Id = Turns.University_Id
                                            inner join Class_Room on Class_Room.Id = Turns.Class_Room_Id
                                            inner join Humen on Humen.Id = Turns.Student_Id

                                            where Class_Room.Name = @P1 AND Turns.Houre = @P2 AND Lessons.Lesson_Code = @P3 AND Turns.Term = @P4 AND Turns.Year = @P5 AND 
                                            Turns.Teacher_Id = @P6 AND Universities.Name = @P7");
    SQL_Com.Connection = SQL_Con;
    SQL_Com.Parameters.Add(new SqlParameter("@P1", "%" + ClassRoom_Name + "%"));
    SQL_Com.Parameters.Add(new SqlParameter("@P2", "%" + Houre + "%"));
    SQL_Com.Parameters.Add(new SqlParameter("@P3", "%" + Lesson_Code + "%"));
    SQL_Com.Parameters.Add(new SqlParameter("@P4", "%" + Term + "%"));
    SQL_Com.Parameters.Add(new SqlParameter("@P5", "%" + Year + "%"));
    SQL_Com.Parameters.Add(new SqlParameter("@P6", Teacher_Id ));
    SQL_Com.Parameters.Add(new SqlParameter("@P7", "%" + University_Name + "%"));

    DataSet DS = new DataSet();

    SqlDataAdapter SQL_DA = new SqlDataAdapter();
    SQL_DA.SelectCommand = SQL_Com;
    SQL_DA.Fill(DS);

    return DS.Tables[0];
}

Upvotes: 0

Views: 349

Answers (1)

Saber
Saber

Reputation: 27

Solution

I accidentally realized how simple the solution was,just delete "%"! The symbol "%" is apparently used for the LIKE command

Upvotes: 0

Related Questions