Reputation: 596
Here is my code:
try
{
SqlCommand cmd2 = new SqlCommand(@"INSERT INTO Clienti (parola,nume,prenume,adresa,email,kcal_zilnice) VALUES (@Parola,@Nume,@Prenume,@Adresa,@Email,2000)", conn);
cmd2.Prepare();
cmd2.Parameters.AddWithValue("@Parola", passBox.Text);
cmd2.Parameters.Add("@Nume", SqlDbType.VarChar, 50).Value = nameBox.Text;
cmd2.Parameters.Add("@Prenume", SqlDbType.VarChar, 50).Value = pnameBox.Text;
cmd2.Parameters.Add("@Adresa", SqlDbType.VarChar, 100).Value = adressBox.Text;
cmd2.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = emailBox.Text;
cmd2.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show("Failed! " + exception.Message);
}
MessageBox.Show("User Created!");
I don't know what am I doing wrong here. I even tried to replace @Parola with 'abc' but it does not work. I don't get any error message. Every time I get "User Created", but when I look into the DB I get all the fields NULL(no records where created).
Upvotes: 0
Views: 59
Reputation: 596
So, for anyone that has the same problem Prepare() solved it. Use Add() for params and specify The SqlDbType. Prepare() will not work with AddWithValue(). At the end do Prepare() and then ExecuteNonQuery().
Upvotes: 1