Unable to call methods on nvarchar

I have a weird problem with a simple SELECT statement that I use in my asp.net application. I work with MSSQL 2008.

This statement works:

SelectSQL = "SELECT user_id, user_name, user_surname, user_code FROM Users WHERE user_group = '" + drop.SelectedItem.Value + "'";

however, this line calls "Unable to call methods on nvarchar".

SelectSQL = "SELECT COUNT(DISTINCT Equations.eq_id) AS pocet_prikladu, Users.user_name, User.user_surname FROM Users LEFT JOIN Equations ON (Users.user_id = Equations.eq_user_id) WHERE Users.user_code = '" + drop.SelectedItem.Value + "' GROUP BY Users.user_id, Users.user_name, User.user_surname ";

here is more code, it stops on the last line

public void FillTable(Table tab, DropDownList drop)     //naplneni tabulky
    {
        SqlConnection pripojeni = new SqlConnection(connectionString);
        string SelectSQL = "";
        if (action == "groups")
        {
            SelectSQL = "SELECT user_id, user_name, user_surname, user_code FROM Users WHERE user_group = '" + drop.SelectedItem.Value + "'";
        }
        else
        {
            SelectSQL = "SELECT COUNT(DISTINCT Equations.eq_id) AS pocet_prikladu, Users.user_name, User.user_surname FROM Users LEFT JOIN Equations ON (Users.user_id = Equations.eq_user_id) WHERE Users.user_code = '" + drop.SelectedItem.Value + "' GROUP BY Users.user_id, Users.user_name, User.user_surname ";
        }
        try
        {
            SqlCommand prikaz = new SqlCommand(SelectSQL, pripojeni);
            pripojeni.Open();
            SqlDataReader vysledky = prikaz.ExecuteReader();
+3
source share
1 answer

You may need to distinguish the user as [user]. The user is a reserved keyword.

So [User] .user_surname, etc.

Also, as Chris says, the typo changes to User.user_surname to Users.user_surname.

I assume that the unanswered problem [User] is causing a strange error

+4
source

All Articles