0

I rewrite my add to database code and now when I want to add data into database I have this error

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: String or binary data would be truncated.

The statement has been terminated.

And here you have my part of

if (Parola.Text != rparola.Text)
        {
            MessageBox.Show("Parolele nu se potrives!");
            return;
        }
        string emailCheck = Email.Text;

        bool valid = false;
        int count_dot = 0;

        for (int i = 0; i < emailCheck.Length; i++)
            if (emailCheck[i] == '@') valid = true;
            else if (emailCheck[i] == '.') count_dot++;

        if (valid == false || count_dot == 0)
        {
            MessageBox.Show("Email invalid!");
            return;
        }

        con = new SqlConnection(@"Data Source=MARIA-PC;Initial Catalog=Account;Integrated Security=True");
        con.Open();
        cmd = new SqlCommand("INSERT into [dbo].[Cont] (Nume_utilizator,Parola,Nume,Prenume,Data_nasterii,Email,Ocupatia) VALUES (@Nume_utilizator,@Parola,@Nume,@Prenume,@Data_nasterii,@Email,@Ocupatia)", con);
        cmd.Parameters.Add("@Nume_utilizator", Nume_utilizator.Text);
        cmd.Parameters.Add("@Parola", Parola.Text);
        cmd.Parameters.Add("@Nume", Nume.Text);
        cmd.Parameters.Add("@Prenume", Prenume.Text);
        cmd.Parameters.Add("@Data_nasterii", DateTime.Parse(Data_nasterii.Text));
        cmd.Parameters.Add("@Email", Email.Text);
        cmd.Parameters.Add("@Ocupatia", Ocupatie.SelectedItem.ToString());
        cmd.ExecuteNonQuery();

        con.Close();

Whitout password checker (check if password and retype password are the same) and email checher (check if email have email structure) my application is working, but when I checked this two I received the error.

I search on the internet to fix it, but I can't and now I need your help to explain me what can I do to resolve my problem.

Here you have a photo with my table

What do you think I can do?

Community
  • 1
  • 1
MariaD
  • 49
  • 1
  • 8
  • It means you are trying to insert data that is bigger than the column width (most commonly on `varchar` columns). You'd have to debug to know _which_ column and know _what_ the improper value is.) – D Stanley Apr 18 '16 at 20:28
  • @DStanley I edited my question. There you can see a photo with my table. There I have: Username, Password, First Name, Last Name, Date of birth, Email, Occupation (work) – MariaD Apr 18 '16 at 20:40
  • I can't see the photo (blocked where I'm at), but you need to look at the length of each column and the length of the data you're trying to insert. – D Stanley Apr 18 '16 at 20:41
  • Open the table in design mode in the database, check each column data type size, than compare it with the size/length of the data you are saving by debugging the code to know the exact values. It will give you the vaue that is bigger in size as compare to the database column size. You can then increase the size of the column in db or truncate your value to the max size of the column. – sumngh Apr 18 '16 at 20:41
  • 1
    try to increase the size of the Email field, as it looks to small to save the email. – sumngh Apr 18 '16 at 20:43
  • @SumerSingh that was the problem. I checked every field and I discoverd the Email field was the problem. Now all is good and is functioning. – MariaD Apr 19 '16 at 15:56

0 Answers0