[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-03-20
4
Medium Priority
?
1,352 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:Murray Brown
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 668 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 668 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 664 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:Murray Brown
ID: 37763064
thanks for the help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

873 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