Solved

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

Posted on 2012-03-20
4
1,277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form Data Disappears Using Back Button 4 65
Stop Git from being my repository 1 47
Display info from DB to Label in asp.net 7 44
EF5 How do I stop pre-compiled views? 8 51
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ
Suggested Courses

737 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