0

I want to find the old co-ordinate and replace it with a new co-ordinate in my Database using C# WPF. I got this error

String or binary data would be truncated.\r\nThe statement has been terminated

and I couldn't find what's the problem. This is my method that I used:

 public void updateEvent(string oldCord,string newCord, DateTime dropDate)
    {

        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "UPDATE Event SET Date = @newDate ,Cordinate=@newCord  WHERE  Cordinate = @oldCord";
            cmd.Parameters.AddWithValue("@newDate", dropDate);
            cmd.Parameters.AddWithValue("@newCord", newCord);
            cmd.Parameters.AddWithValue("@oldCord", oldCord);             
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = connection;
            cmd.ExecuteNonQuery();
        }
    }
4
  • 1
    This exception is thrown because you're trying to write more data into a field of a table than would fit into it. Like a string with 100 characters into a varchar(50) field. So the question is, how exactly is the field Cordinate (should probably be named Coordinate) defined and how long exactly is the newCord you try to write into it? Commented Apr 24, 2016 at 6:24
  • 1
    You should check out Can we stop using AddWithValue() already? and stop using .AddWithValue() - it can lead to unexpected and surprising results... Commented Apr 24, 2016 at 6:30
  • How about this post 'stackoverflow.com/questions/17312558/…'? Commented Apr 24, 2016 at 6:32
  • We need to see the table declaration in order to know the length of the column values. Commented Apr 24, 2016 at 6:51

1 Answer 1

1

Generally the cause for this issue is that the length of the parameter you send to the stored procedure is bigger than the size of the field in the actual table in the database.

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

Comments

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.