Solved

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

Posted on 2012-03-20
4
1,201 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

11 Experts available now in Live!

Get 1:1 Help Now