C# Simple example of using a reader to loop through fields as in

Hi. I am new to C#. Could someone please direct me to a code example of reading through a SQL database in a similar fashion to the code below

  Protected Sub Login_Button_Click(sender As Object, e As EventArgs) Handles Login_Button.Click

        Dim oAdminID As String
        Dim oPassword As String
        Dim SQL As String

        Dim myConnection As SqlConnection = New SqlConnection("server=196cc;uid=Murray;pwd=cct;database=sgg")

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

        SQL = "SELECT * From Administrators"
        Dim command As SqlCommand = New SqlCommand(SQL, myConnection)


            Dim reader As SqlDataReader = command.ExecuteReader()

            If reader.HasRows Then

                Do While reader.Read()
                    If Not reader("AdminID").Equals(DBNull.Value) Then
                        oAdminID = reader("AdminID").ToString()
                        oAdminID = ""
                    End If
                    If Not reader("Password").Equals(DBNull.Value) Then
                        oPassword = reader("Password").ToString()
                        oPassword = ""
                    End If

                    If oAdminID = Me.AdminID.Text Then
                        If oPassword = Password.Text Then

                            Me.Label_LoggedIn.Text = "Logged in:"
                            Me.Label_Reviewer.Text = oAdminID

                            Me.Label_Error1.Text = ""
                            Me.Panel1.Visible = True

                            Me.Label_Error1.Text = "The password that you entered does not match the Reviewer namee."
                        End If
                        Me.Label_Error1.Text = "The username entered does not exist"
                    End If

                'Console.WriteLine("No rows found.")
            End If



        Catch ex As Exception
            Me.Label_Error1.Text = ex.Message
        End Try
    End Sub
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
Lalit ChandraConnect With a Mentor Commented:
try this
string oAdminID = null;
string oPassword = null;
string SQL = null;

SqlConnection myConnection = new SqlConnection("server=196cc;uid=Murray;pwd=cct;database=sgg");

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

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

try {

      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.")



} catch (Exception ex) {
      this.Label_Error1.Text = ex.Message;
Anurag ThakurTechnical ManagerCommented:
There are free code conversion sites available on the Internet which convert code from to c# and vice versa.

In an answer to your another question I have give a link for C# tutorial on and that will also be helpful for you.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.