Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:37:40+00:00 2026-06-05T11:37:40+00:00

After the query execution the result I need to take is UserNames part. I’m

  • 0

After the query execution the result I need to take is UserNames part.

I’m gettting an error: Cannot apply indexing with [] to an expression of type ‘method group’

public class ProcessInput
{
    string connectionString = (string)ConfigurationSettings.AppSettings["myConString"];
    private string msg_arr;
    string username = null;

    private void MooseSeenInput(string MobileNo, string Date, string odd, params Array[] msg_arr)
    {
        SqlConnection conn = new SqlConnection(myConString);
        conn.Open();
        SqlCommand com = new SqlCommand("SELECT * FROM Users as users WHERE UserName=@UserName AND State!='1' AND State!='4'", conn);
        com.Parameters.AddWithValue("@UserName", UserName);
        // com.ExecuteNonQuery();
        using (SqlDataReader reader = com.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    String UserNames = (reader.Read["users"]);
                }
            }
        }
    }
}

Error occurred at line:

String UserNames = (reader.Read["users"]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:37:42+00:00Added an answer on June 5, 2026 at 11:37 am

    Your error is a compiler error as you were trying to apply index syntax onto a method call, which in C# involves brackets.

    The SqlDataReader does expose an indexer, but it is on the reader itself, not a method member:

    string username = (string)reader["users"];
    

    This is how I tend to style this code:

    using (SqlDataReader reader = com.ExecuteReader())
    {
        int userOrd = reader.GetOrdinal("users");
    
        while (reader.Read())
        {
            string username = reader.GetString(userOrd);
        }
    }
    

    Note that I ask the reader for the ordinal prior to iterating an unknown number of records. This is good for two reasons:

    1. I have cached the ordinal once and use it many times, this helps performance.
    2. I have not hard-coded the ordinal so if someone were to change the column position in the result-set in the stored procedure / script, my code would continue to work.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I view the warning(s) after a SELECT query? The result of one
Keep getting this error after inserting a subdatasheet into a query and trying to
In Java, after executing a query say I got a result set like: bat
Is there any reason why I should close the connection after a query or
After a Select Query I have the following table: id CN SN DC 1
Update: After playing around with this for a few hours, went with a multi-query
In my query, I use the IIF function to assign either Before or After
how I can format my database-query in a list view with 2 signs after
I am doing a really simple query in ASP.NET, but after I inserted the
I have a problem with a relatively simple query and the execution plan Access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.