Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to match the users ID (pk) on the 'users' table, with the users ID (fk) on the 'owners' table, but it seems to not be working.

I got a sign up form, which is going to insert all the data into both this tables.

But it gives me and error on the dono (onwer) saying that I need to convert it.

What I have tried:

I got the following code:

C#
protected void confirmarButton_Click(object sender, EventArgs e)
        {
           // int n = 0;

            if (primeiroNome.Text != "" & nomeDoMeio.Text != "" && sobrenome.Text != "" && dataDeNascimento.Text != "" && enderecoPostal1.Text != "" && cidade.Text != "" && email.Text != "" && username.Text != "" && password.Text != "" && confirmarPassword.Text != "")
            {
                if (password.Text == confirmarPassword.Text)
                {
                    String CS = ConfigurationManager.ConnectionStrings["ClinicaAnimal"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(CS))
                    {

                       // n = userIdNumber();


                        SqlCommand cmd2 = new SqlCommand("Insert into Users ([Username], [Password],[Tipo de User])Values('" + username.Text + "','" + Encrypt(password.Text.Trim()) + "','" + "Dono" + "')", con);
                       // cmd2.Parameters.AddWithValue("@Password", Encrypt(password.Text.Trim()));
                        SqlCommand cmd = new SqlCommand("Insert into Dono ([Primeiro Nome], [Nome do Meio], [Sobrenome], [Data de Nascimento], [Endereço Postal1], [Endereço Postal2], [Cidade], [Email], [UserID])Values('" + primeiroNome.Text + "','" + nomeDoMeio.Text + "','" + sobrenome.Text + "','" + dataDeNascimento.Text + "','" + enderecoPostal1.Text + " ','" + enderecoPostal2.Text + " ','" + cidade.Text + " ','" + email.Text + "','"+ "SELECT SCOPE_IDENTITY()" +"')", con);
                       

                        con.Open();

                        cmd2.ExecuteNonQuery();
                        cmd.ExecuteScalar();
                        

                        Response.Redirect("~/Home.aspx");
                    }
                }
                else
                {
Posted
Updated 10-Feb-18 9:29am
Comments
Richard Deeming 13-Feb-18 12:23pm    
In addition to the critical SQL Injection vulnerability in your code, you should NEVER store passwords using reversible encryption. You should be using a secure one-way hash algorithm, with a unique salt for each record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

Quote:
But it gives me and error on the dono (onwer) saying that I need to convert it.

Try to tell exact error message ans position.

C++
SqlCommand("Insert into Users ([Username], [Password],[Tipo de User])Values('" + username.Text + "','" + Encrypt(password.Text.Trim()) + "','" + "Dono" + "')", con);

Not 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[^]
 
Share this answer
 
v2
Hey there,

Best guess from reading your question is that you're having trouble with type casting[^] :)

C# (and SQL, for that matter) will often freak out if you try to use the wrong data type (ie, string instead of int/double/float) in a statement.

For example:
C#
int eight = 0;   // works
eight = "0";     // won't work
eight = 5 + 3;   // works
eight = 5 + "3"; // won't work
There isn't quite enough information in your question to tell what kind of casting you have to do, but you'll probably have to do something along the lines of this:
C#
// converts the "string" data type to the "int" data type
int casted_value = (int)textbox.Text;
Keep in mind that casting a variable from one type to another will cause unwanted runtime errors if the data being entered is malformed in any way - for example:
C#
string five = "five";

// will throw an error, C# can't recognize spoken words :(
int converted_five = (int)five;
To mitigate this risk, make sure you programmatically validate and clean all data before attempting type casting.

Lastly, as ppolymorphe mentioned above - directly concatenating SQL queries using strings has enormous security risks, and can/will lead to your entire database being leaked / corrupted / misused as soon as you deploy your application :)
 
Share this answer
 
v3

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