Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnRegister_Click(object sender, EventArgs e)
{
    string cs = @"Data Source=.\SQLEXPRESS; 
                AttachDbFilename=|DataDirectory|\Database.mdf; 
                Integrated Security=True; 
                User Instance=True";

    string sql = "INSERT INTO Member (Username, UserPassword, Name, IC, Gender, Address, Email) VALUES (@Username, @Password, @Name, @IC, @Gender, @Address, @Email)";

    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand(sql, con);

    cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
    cmd.Parameters.AddWithValue(Encrypt("@Password"), txtPassword.Text);
    cmd.Parameters.AddWithValue("@Name", txtName.Text);
    cmd.Parameters.AddWithValue("@IC", txtIC.Text);
    cmd.Parameters.AddWithValue("@Gender", ddlGender.Text);
    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
    cmd.Parameters.AddWithValue("@Email", txtEmail.Text);

    con.Open();        
    int number =  cmd.ExecuteNonQuery();//this line has a problem
    con.Close();

    if (number >= 1)
    {
        lblResult.Text = number + " Success Insert";
    }
    else
    {
        lblResult.Text = "Please try again";
    }
}

private string Encrypt(string clearText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            clearText = Convert.ToBase64String(ms.ToArray());
        }
    }
    return clearText;
}
Posted
v2

This:

C#
cmd.Parameters.AddWithValue(Encrypt("@Password"), txtPassword.Text);


Should probably be:

C#
cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text));


Otherwise with your original code, you are telling it to encrypt the parameter name not the text in the textbox. The encrypted parameter name does not match the defined name and the command can't find the parameter named @Password anymore.
 
Share this answer
 
v2
Comments
+5. We added answers almost at the same time.
Karthik_Mahalingam 17-Dec-13 10:15am    
+5 for your ethical behavior, encouraging the colleagues, Good man.
C#
cmd.Parameters.AddWithValue(Encrypt("@Password"), txtPassword.Text);

Here you should Encrypt the Value not the Parameter.

So, change it to...
C#
cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text));
 
Share this answer
 
Comments
Ron Beyer 17-Dec-13 10:11am    
+5 for having a keyboard a couple milliseconds faster than mine :)
Karthik_Mahalingam 17-Dec-13 10:13am    
:)
Thanks a lot Ron Beyer... :)
You are encrypting the parameter name instead of the password value,

Try this

C#
cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text));
 
Share this answer
 
v2
Comments
Ron Beyer 17-Dec-13 10:23am    
+5, correct.
Karthik_Mahalingam 17-Dec-13 10:24am    
Thanks Ron
try this code..
i was correct a single line its may be helpful for you!!!

C#
string encrypted=Encrypt(txtPassword.Text);
cmd.Parameters.AddWithValue("@Password",encrypted);
 
Share this answer
 
v2
Copying and running the query in SQL server should normally help you resolve such issues.
 
Share this answer
 
just remove userpassword instead of it write password only it will solve
 
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