Solved

ASP.net C# "reader is a variable but is used like a method"

Posted on 2012-03-20
4
1,228 Views
Last Modified: 2012-03-25
Hi. I am trying to use the following code to read through a SQL table to check if a userID and password exist. I converted the code from a VB.net project and am getting an error on the word reader - "reader is a variable but is used like a method"



        protected void Button1_Click(object sender, EventArgs e)
        {
            {
                string oAdminID = null;
                string oPassword = null;
                string SQL = null;

                SqlConnection myConnection = new SqlConnection("server=......;uid=Murray;pwd=.....;database=.....y");

                //Dim command As SqlCommand = New SqlCommand("SELECT * From Reviewers;", myConnection)

                SQL = "SELECT * From Administrators";
                SqlCommand command = new SqlCommand(SQL, myConnection);

                try
                {
                    myConnection.Open();

                    SqlDataReader reader = command.ExecuteReader();


                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            if (!reader("AdminID").Equals(DBNull.Value))
                            {
                                oAdminID = reader("AdminID").ToString();
                            }
                            else
                            {
                                oAdminID = "";
                            }
                            if (!reader("Password").Equals(DBNull.Value))
                            {
                                oPassword = reader("Password").ToString();
                            }
                            else
                            {
                                oPassword = "";
                            }


                            if (oAdminID == this.AdminID.Text)
                            {

                                if (oPassword == Password.Text)
                                {
                                    this.Label_LoggedIn.Text = "Logged in:";
                                    this.Label_Reviewer.Text = oAdminID;


                                    this.Label_Error1.Text = "";
                                    this.Panel1.Visible = true;

                                }
                                else
                                {
                                    this.Label_Error1.Text = "The password that you entered does not match the Reviewer namee.";
                                }
                            }
                            else
                            {
                                this.Label_Error1.Text = "The username entered does not exist";
                            }

                        }
                    }
                    else
                    {
                        //Console.WriteLine("No rows found.")
                    }

                    reader.Close();

                    myConnection.Close();

                }
                catch (Exception ex)
                {
                    this.Label_Error1.Text = ex.Message;
                }
            }

        }
0
Comment
Question by:murbro
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 167 total points
ID: 37744900
In C# arrays are accessed with square brackets. For example:
reader["AdminID"]
0
 
LVL 7

Assisted Solution

by:Lalit Chandra
Lalit Chandra earned 167 total points
ID: 37745350
Square brackets, not parentheses, should be used when referencing a column

Reader("AdminID");
should be
Reader["AdminID"];
0
 
LVL 9

Assisted Solution

by:darjimaulik
darjimaulik earned 166 total points
ID: 37746192
Hi,
If you want to access Field item of the Reader in C#
you have to use reader["value"] like below

						
while (reader.Read())
	{
		if (!reader["AdminID"].Equals(DBNull.Value))
		{
			oAdminID = reader["AdminID"].ToString();
		}
		else
		{
			oAdminID = "";
		}
		if (!reader["Password"].Equals(DBNull.Value))
		{
			oPassword = reader["Password"].ToString();
		}

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 37763064
thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now