Solved

ASP.net C# Simple example of using a reader to loop through fields as in VB.net

Posted on 2012-03-20
3
378 Views
Last Modified: 2012-03-25
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 VB.net 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)
        Try

            myConnection.Open()

            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()
                    Else
                        oAdminID = ""
                    End If
                    If Not reader("Password").Equals(DBNull.Value) Then
                        oPassword = reader("Password").ToString()
                    Else
                        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

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

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

            reader.Close()

            myConnection.Close()

        Catch ex As Exception
            Me.Label_Error1.Text = ex.Message
        End Try
    End Sub
0
Comment
Question by:murbro
3 Comments
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
ID: 37744931
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 {
      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
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 37745030
There are free code conversion sites available on the Internet which convert code from vb.net to c# and vice versa.

In an answer to your another question I have give a link for C# tutorial on ado.net and that will also be helpful for you.
0
 

Author Closing Comment

by:murbro
ID: 37763069
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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