Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi my name is vishal i was wondering of how to execute a sql command within data reader execution of another sql command(in while loop) in c# windows forms with sql server 2008.
So i have login form named:frmLogin and given below is it's c# code:
C#
namespace Mini_Project
{
    public partial class frmLogin : Form
    {
public frmLogin()
        {
            InitializeComponent();
        }
private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
private void btnLogin_Click(object sender, EventArgs e)
        {
            if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
            {
                Module.AUser_ID=1;
                MDIParent1 h = new MDIParent1();
                h.Show();
                this.Close();
            }
            else
{
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                bool validUser = ValidateUser(username, password);
if (validUser)
                    {
                        Module.User_ID = 1; 
                        MDIParent1 m = new MDIParent1();
                        m.Show();
                        this.Close();
                    }
else
                    {
                        MessageBox.Show("Invalid user name or password. Please try tomorow ", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtUsername.Focus();
                    }
} private bool ValidateUser(string username, string password)
        {
            bool success = false;
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select * from [dbo].[UserDetail2] where username='"+txtUsername.Text+"' and password='"+txtPassword.Text+"' and LoginAttempts< 3", conn);
            SqlDataReader rd = cmd.ExecuteReader();
           while(rd.Read())
            {
                success=true;
                Module.User_ID = Convert.ToInt32(rd[0].ToString());
                Module.UserName = rd[1].ToString();
            }
            rd.Close();
            conn.Close();
            return success;
        }
    }
}

The above c# code works OK to some extent!

Given below is my c# code of my class named:Module
C#
using System.Threading.Tasks;
namespace Mini_Project
{
    class Module
    {
 public static int User_ID;
public static string UserName;
}
}

What i want is since i am developing a login which enables a user to enter into application(Mini Project) with LoginAttempts less than 3(2 unsuccessful attempts) and if LoginAttempts of that user is greater than 3 then it blocks that user from entering into application(Mini Project) via login form(frmLogin).

So i want is execute a sql command after successful login.Given below is that sql command:
C#
 cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
cmd.ExecuteNonQuery();


I also want to execute another sql command after unsuccessful login.Given below is that sql command:
C#
cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
               cmd.ExecuteNonQuery();


Can anyone tell me/guide me/help me on how to implement this function in/inside function(ValidateUser) in my login form(frmLogin)? Or is there another way to achieve this required result then Please help me/guide me?!
Can anyone tell me what modifications must i need to do in my c# code of login form(frmLogin) to achieve my required result!
Can anyone help me Please! Any help/guidance in solving of this problem would be greatly appreciated!
Posted

1 solution

C#
if (validUser)
                    {
                    cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                        Module.User_ID = 1;
                        MDIParent1 m = new MDIParent1();
                        m.Show();
                        this.Close();

                    }
else

else
                    {
                cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
                cmd.ExecuteNonQuery();
                        MessageBox.Show("Invalid user name or password. Please try tomorow ", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtUsername.Focus();
                    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900