Creating a public property for a class in C#

adamtrask
adamtrask used Ask the Experts™
on
Hello experts,

I am trying to create a public property for a class named ListsDB

The purpose of this public property is to retrieve data from a table in a database

I believe the code is ok except for the  following line which generates an error message:

 myCallsList.Add (reader("Call"));

The error message simply says "reader" is a variable but is used like a method.

myCallsList is a strongly typed string List and I am quite at loss as to how to rewrite the line which generates the error.

Thanks



public void getAllCalls()
    {
        SqlConnection con = default(SqlConnection);
        SqlCommand comm = default(SqlCommand);
               con = new SqlConnection("Server=Tariq; Database=HelpDesk;Integrated Security=True");
        comm = new SqlCommand("Select * from Calls", con);
        con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
              myCallsList.Add (reader("Call"));
            }
            reader.Close();
            con.Close();

        }
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There is an error in your code .. Try to modify the code this way :
public void getAllCalls()
    {
        SqlConnection con = default(SqlConnection);
        SqlCommand comm = default(SqlCommand);
               con = new SqlConnection("Server=Tariq; Database=HelpDesk;Integrated Security=True");
        comm = new SqlCommand("Select * from Calls", con);
        con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
              myCallsList.Add (reader["Call"]);
            }
            reader.Close();
            con.Close();

        }
    }

Open in new window

Notice that i have used square brackets [] instead of Round ones ()
Also if the above code doesn`t work try this :
public void getAllCalls()
    {
        SqlConnection con = default(SqlConnection);
        SqlCommand comm = default(SqlCommand);
               con = new SqlConnection("Server=Tariq; Database=HelpDesk;Integrated Security=True");
        comm = new SqlCommand("Select * from Calls", con);
        con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
              myCallsList.Add (reader["Call"].ToString());
            }
            reader.Close();
            con.Close();

        }
    }

Open in new window

anarki_jimbelSenior Developer

Commented:
I believe it's not bad place to check for proper syntax you may use:

http://www.sharpdeveloper.net/content/archive/2007/06/09/how-to-use-sqldatareader-plus-source-code.aspx

So, try:
             myCallsList.Add (reader.GetString(reader.GetOrdinal("FirstName")));


I didn't run this code

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial