0

I have an insertion query in this function :

public string Insert_Piece(List<Piece> liste)
        {
            this.Connect();
            using (connexion)
            {

                using (SqlCommand sqlCmd = new SqlCommand("INSERT INTO Piece (IDPiece, IDSuperlot, Url) VALUES (@idpiece, @idsuperlot, @url)", connexion))
                {
                    foreach (Piece p in liste)
                    {
                        sqlCmd.Parameters.AddWithValue("@idpiece", p.Id_piece);
                        sqlCmd.Parameters.AddWithValue("@idsuperlot", p.Id_super_lot);
                        sqlCmd.Parameters.AddWithValue("@url", p.Url_piece);
                        try
                        {
                            sqlCmd.ExecuteNonQuery();
                        }
                        catch (Exception e) { return e.ToString(); }

                    }
                    return "cava";
                }
            }
        }

But always an exception appears:error

I don't know what is the problem and how can i fix it . The 3 attributs are string (varchar) and the selection queries works fine without problem.

  • What is the matter?
  • How can i fix it?
3
  • can you please translate the error in English, that will be very helpful. Commented May 8, 2013 at 23:17
  • 2
    I think it's "the string or binary data would be truncated" Commented May 8, 2013 at 23:18
  • So, one of your strings is too long for the column you are trying to insert it into (URL, probably) Commented May 8, 2013 at 23:19

1 Answer 1

3

It looks like the issue is you are trying to insert too long of a string into the varchar column, try making the varchar column larger or changing it to be a text column.

Sign up to request clarification or add additional context in comments.

2 Comments

how can i change th type of the field url to text
Well @Lamloumi , open up whatever you used to initially create the database, I assume SQL Server Management Studio and on the dropdown just change varchar to text, or just change the number in the parentheses on the varchar to MAX and see if that fixes it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.