if statements with 2 conditions

c#, if-statement

Solution

You seem to be mixing LINQ and SQL.

Replace `&&` with `AND`.

And you may also have to remove the single apostrophes from around idQuiz, if it's actually a number and not a string.

Also, we can't see the rest of your code, but you'll want to look into parameterzing your queries instead of concatenating them into one string. It's safer, and easier to read and maintain.

This is on-the-fly, so there may be some syntax errors. It doesn't follow your example exactly - I'm not sure what kind of logic you have in the `Global` class.

private int GetQuestionIds(string content, int quizId)
{
    List<int> idQuestions = new List<int>();

    string query
        = "SELECT ID FROM Questions WHERE (content = @content AND Quiz_ID = @quizId)";

    using (var connection = new SqlConnection(connectionString))
    {
        var command = new SqlCommand(query, connection);
        command.Parameters.AddWithValue("@content", content);
        command.Parameters.AddWithValue("@quizId", quizId);

        try
        {
            connection.Open();

            var reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));

                    idQuestions.Add(idQuestion);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    return idQuestions.Any() ? idQuestions.First() : -1;
}

You can find more info on MSDN.

Problem

I am really confused why my script always give error result I want to make 'if section' with multiple conditions like this ``` string kalimatsql2 = "SELECT ID FROM Questions WHERE (content = '" + var + "'" + " && Quiz_ID = '" + idQuiz + "')"; ``` I already tried to change place single quote, change `=` to `==`, ommited `()`, dll but still give error Syntax error (missing operator) in query expression '(content = 'test2' && Quiz_ID = '6')'. Update ``` string kalimatsql2 = "SELECT ID FROM Questions WHERE (content = '" + dataDel + "'" + " AND Quiz_ID = " + idQuiz + ")"; int idQuestion = sqlReader(kalimatsql2); ``` and this is code for sqlReader function ``` private int sqlReader( string kalimatSql) { Global.dbCon.Open(); List<int> idQuestions = new List<int>(); Global.reader = Global.riyeder(kalimatSql); if (Global.reader.HasRows) { while (Global.reader.Read()) { int idQuestion = Convert.ToInt32(Global.reader.GetValue(0)); idQuestions.Add(idQuestion); } } Global.dbCon.Close(); foreach (int id in idQuestions) { return id; } return (idQuestions.Count > 0) ? idQuestions[0] : -1; } ``` i used microsoft access as database

Original source