0

Let say there is an Asp.ne5 3.5 web application with an Access database. The database has two tables like this:

enter image description here

In some pages I can get the username with `"page.users.identity.name". In this Application, each user can create so many pages. My question is how to write a statement to get pages created with specific username (the Group_ID in joined).

NOTE: This is what i tried and i got "error reading database".

SELECT Pages.* FROM 
Pages INNER JOIN Users ON Pages.Group_ID = Users.Group_ID
WHERE Users.Username = page.users.identity.name

3 Answers 3

1

Have you tried using a parametrized query:

public ActionResult SomeAction()
{
    // get the username of the currently connected user
    string username = User.Identity.Name;

    string cs = WebConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString;
    using (var conn = new OleDbConnection(cs))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        var sql = 
            @"SELECT Pages.* FROM Pages 
              INNER JOIN Users 
              ON Pages.Group_ID = Users.Group_ID
              WHERE Users.Username = ?";
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("Username", username);
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                ... 
            }
        }
    }

    ...

}
Sign up to request clarification or add additional context in comments.

Comments

0

Try with this query,

SELECT Pages.* FROM 
Pages,Users where Pages.Group_ID = Users.Group_ID
and Users.Username = page.users.identity.name

Comments

0

I Missed the Single quote "' " in the statement for page.users.identity.name. now this should be like this:

dim lasts as string= "'"

"SELECT Pages.* FROM 
Pages INNER JOIN Users ON Pages.Group_ID = Users.Group_ID
WHERE Users.Username ='" &  page.users.identity.name & lasts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.