Question

I have this windowadmin table. I would like to hardcode it for the 4th record in the table. If user key in username = Admin and password = 123, log him in. Below are my images and the codes. Thank you very much.

enter image description here

private void btnAdminLogin_Click(object sender, EventArgs e)
{
    //retrieve connection information info from App.config
    string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
    //STEP 1: Create connection
    SqlConnection myConnect = new SqlConnection(strConnectionString);
    //STEP 2: Create command
    //For WindowsAdmin
    string strCommandtext = "SELECT * from WINDOWSADMIN";
    strCommandtext += "   WHERE winUsername=@aname AND winPassword=@apwd;";

    SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
    //For WindowsAdmin
    cmd.Parameters.AddWithValue("@aname", txtAdminUsername.Text);
    cmd.Parameters.AddWithValue("@apwd", txtAdminPasswd.Text);


    try
    {
        // STEP 3: open connection and retrieve data by calling ExecuteReader
        myConnect.Open();
        // STEP 4: Access Data
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read()) //For WindowsAdmin
        {
            if (MessageBox.Show("Login Successful") == DialogResult.OK)
            {
                admin form = new admin(txtAdminUsername.Text);
                form.Show();
                /*
                login loginForm = new login();
                this.Visible = false;
                this.Hide();
                adminLogin AdminloginForm = new adminLogin();
                this.Visible = false;
                this.Hide();
                */
                return;
            }
        }


        //STEP 5: close connection
        reader.Close();
        MessageBox.Show("Invalid username or password ");

    }
}
Was it helpful?

Solution

I think this should be your code

private void btnAdminLogin_Click(object sender, EventArgs e)
{
    if (txtAdminUsername.Text=="<hardcoded_Username>" && txtAdminPasswd.Text=="<hardcoded_Password>")
    {
         //Login as Hardcoded User
         MessageBox.Show("Login Successful") == DialogResult.OK
         //Do your stuff

         return;
    }
    //retrieve connection information info from App.config
    string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
    //STEP 1: Create connection
    SqlConnection myConnect = new SqlConnection(strConnectionString);
    //STEP 2: Create command
    //For WindowsAdmin
    string strCommandtext = "SELECT * from WINDOWSADMIN";
    strCommandtext += "   WHERE winUsername=@aname AND winPassword=@apwd;";

    SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
    //For WindowsAdmin
    cmd.Parameters.AddWithValue("@aname", txtAdminUsername.Text);
    cmd.Parameters.AddWithValue("@apwd", txtAdminPasswd.Text);



    try
    {
        // STEP 3: open connection and retrieve data by calling ExecuteReader
        myConnect.Open();
        // STEP 4: Access Data
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read()) //For WindowsAdmin
        {
            if (MessageBox.Show("Login Successful") == DialogResult.OK)
            {
                admin form = new admin(txtAdminUsername.Text);
                form.Show();
                /*
                login loginForm = new login();
                this.Visible = false;
                this.Hide();
                adminLogin AdminloginForm = new adminLogin();
                this.Visible = false;
                this.Hide();
                */
                return;
            }
        }


        //STEP 5: close connection
        reader.Close();
        MessageBox.Show("Invalid username or password ");

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top