Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When i run my application, it goes through to the end just fine, but when i check my DB afterwards, it never shows any data. Here is my code:

private void button1_Click(object sender, EventArgs e)
    {
        string saltedcryps = saltpassword(10);
        string passWithSalt = (textBox1.Text + saltedcryps);
        string hashedResult = hashPassAndSalt(passWithSalt);
        if (checkPasswordsMatch() == "B")
        {
            SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf");
                    try
                    {
                        myConnection.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');", myConnection);
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                    this.Hide();

        }
    }
    private string checkPasswordsMatch()
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("Passwords cannot be empty");
            return "A";
        }
        else
        {
            MessageBox.Show(textBox1.Text == textBox2.Text ? "Thanks for registering!" : "Your passwords do not match");
            return "B";
        }
    }
    private string saltpassword(int size)
    {
        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        byte[] buff = new byte[size];
        crypto.GetBytes(buff);
        return Convert.ToBase64String(buff);
    }
    private string hashPassAndSalt(string passWithSalt)
    {
        HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();
        byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(passWithSalt);
        byte[] bytHash = hashAlg.ComputeHash(bytValue);
        string base64 = Convert.ToBase64String(bytHash);
        return base64;
    }
}

It is the button1_Click that the problem lies in. When it runs myCommand.ExecuteNonQuery(); it never throws an exception, it just carries on, without actually entering any information...

Anyone have a clue??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

    if (checkPasswordsMatch() == "B")
        {
            SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf");
                    try
                    {
                        myConnection.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    SqlCeCommand myCommand = myConnection.CreateCommand();
                    myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');"
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                    this.Hide();

        }

If this doesnt works, try to put the absolut path on the connection string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...