0

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];
}
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
Saber
  • 27
  • 7
  • If you're receiving an error, please edit your question and add the error. – devlin carnate Jun 09 '21 at 17:20
  • there is no error!just does not show information in datagridview – Saber Jun 09 '21 at 17:25
  • It doesn't look like you're executing the SQL... – devlin carnate Jun 09 '21 at 17:27
  • I executed this code in sql and it works properly but I do not know what is the problem with ado.net? – Saber Jun 09 '21 at 17:30
  • Check out [this thread](https://stackoverflow.com/questions/6073382/read-sql-table-into-c-sharp-datatable) – devlin carnate Jun 09 '21 at 17:46
  • I tried it but it did not work! – Saber Jun 09 '21 at 18:13
  • 2
    Your use of `%` makes me think you meant to use `LIKE` rather than `=`? – mjwills Jun 09 '21 at 22:58
  • `I executed this code in sql` No you didn't - that code wouldn't work in direct SQL either. Please show us the SQL that _did_ work and we can highlight the differences to you. – mjwills Jun 09 '21 at 22:59
  • `Class_Room.Name = @P1` for example should probably be `Class_Room.Name LIKE @P1`, also I advise you to pick better parameter names, and use table aliases for readability. You also need to dispose your connection, command, adapter objects with `using` blocks – Charlieface Jun 10 '21 at 14:52
  • I suggest doing an Sql Profiler trace on your ADO.NET request. Then you can paste it into SSMS, and reproduce the problem in SSMS. Also makes it easer to see what ADO.NET is sending to Sql Server. – Moe Sisko Jun 16 '21 at 04:38

1 Answers1

0

Solution

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

Saber
  • 27
  • 7