Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i use this code to change password
C#
private void Button2_Click(object sender, EventArgs e)
        {
            SqlDataAdapter ASDF = new SqlDataAdapter("select count (*) FROM tbluser1 where username ='" + textBox1.Text + "'AND password='" + textBox2.Text + "'", con);
            DataTable DS = new DataTable();
            ASDF.Fill(DS);
            errorProvider1.Clear();
            if (DS.Rows[0][0].ToString() == "1")
            {
                if (textBox3.Text == textBox4.Text)
                {
                    SqlDataAdapter cc = new SqlDataAdapter("update tbluser1 set password ='" + textBox3.Text + "' where username = '" + textBox1.Text + "'and password = '" + textBox2.Text + "'", con);
                    DataTable DF = new DataTable();
                    cc.Fill(DF);
                    MessageBox.Show("Password Changed....", "message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    errorProvider1.SetError(textBox3, "UnMatch Password");
                    errorProvider1.SetError(textBox4, "UnMatch Password");
                }
            }
            else
            {
                errorProvider1.SetError(textBox1, "incorrect User Name");
                errorProvider1.SetError(textBox2, "incorrect Password");
            }
        }


What I have tried:

what i looking for is make the password stop working after 90 days and should change it that is all.
i know that i should have a colame for date for the last change

and that my login code

C#
SqlDataAdapter sda = new SqlDataAdapter("select count(*)  from tbluser1 where username='" + txtusername.Text.Trim() + "' and password='" + txtpassword.Text.Trim() + "'", sqlcon);
            DataTable dtbl = new DataTable();
            sda.Fill(dtbl);



            if (dtbl.Rows[0][0].ToString() == "1")
            {
                SqlDataAdapter From_sda = new SqlDataAdapter("select user_id , username from tbluser1 where username='" + txtusername.Text.Trim() + "' and password='" + txtpassword.Text.Trim() + "'", sqlcon);
                DataTable From_ds = new DataTable();
                From_sda.Fill(From_ds);
                String value1 = From_ds.Rows[0][1].ToString();
                int id = int.Parse(From_ds.Rows[0][0].ToString());

                Debug.WriteLine("value is :   " + value1);
                Class1.Txtusername = txtusername.Text;
                this.Hide();
                SqlDataAdapter sda1 = new SqlDataAdapter("select role , [from], Take, from2, Take2, from3, Take3, from4, Take4 from tbluser1 where username='" + txtusername.Text.Trim() + "' and password='" + txtpassword.Text.Trim() + "'", sqlcon);
                DataTable ds = new DataTable();
                sda1.Fill(ds);
                Researcher obj = new Researcher(ds.Rows[0][0].ToString(), ds.Rows[0][1].ToString(), ds.Rows[0][2].ToString(), ds.Rows[0][3].ToString(), ds.Rows[0][4].ToString(), ds.Rows[0][5].ToString(), ds.Rows[0][6].ToString(), ds.Rows[0][7].ToString(), ds.Rows[0][8].ToString());
                this.Hide();
                obj.Show();
            }
            else
            {
                MessageBox.Show("please check your username and password");
            }
Posted
Updated 22-Jan-19 0:11am

C#
SqlDataAdapter("select count(*)  from tbluser1 where username='" + txtusername.Text.Trim() + "' and password='" + txtpassword.Text.Trim() + "'", sqlcon);

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
el_tot93 22-Jan-19 6:13am    
hey it is windows application no problem
Not like that! Two cardinal sins in one piece of code...

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And then there is how you are storing things ... Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

Fix those first, throughout your app. Then worry about minor problems like password expiry!
 
Share this answer
 
Comments
el_tot93 22-Jan-19 6:12am    
hey it is windows application no problem
Richard Deeming 22-Jan-19 7:14am    
Then simply remove the password protection, since you implicitly trust ALL of your users to only look at and modify the data they're allowed to. 🤦‍♂️

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