Click here to Skip to main content
15,889,528 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i am trying to update, i am getting System.Data.SqlClient.SqlException: 'String or binary data would be truncated. The error comes from ExecuteNonQuery . i am having doubt that can i do two Command operations from same sql connection ?.why is it showing that error? Thanks

What I have tried:

This is my short code , i have included everything .
C#
public string singleloop(string Variable, int qty, string Variablename)
        {
            List<string> data = new List<string>(); 
using (SqlConnection sqlcon = new SqlConnection(@""))
            {
               
                string query = string.Format("select top({0})SUBSTRING(Name,1,17) from Students Where  Grade= '{1}'", qty, Variable);

                sqlcon.Open();
                
                    SqlCommand sqlCommand = new SqlCommand(query, sqlcon);
                
                    //sqlcon.Open();
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    var dataReader = sqlDataReader;
                    while (dataReader.Read())
                    {
                        data.Add(Convert.ToString(dataReader[0]));
                        dataReader.Close();

                        string HR = string.Join(";", data.ToArray());
                        string q = "update Students SET Progress = 'Good' where grade = 'A' ";
                        SqlCommand co = new SqlCommand(q,sqlcon);
                        co.ExecuteNonQuery();
		    }
Posted
Updated 3-Jul-21 20:38pm
v3

1 solution

Simpel: the field length you set in the DB is not big enough for the data you are supplying.

The default size for a VARCHAR or NVARCHAR column is normally 50 characters - if you try to INSERT or UPDATE a string with 51 characters the DB throws the error because it won't fit without it throwing away some of your data.

Either check your data length then truncate or re-enter it, or increase the column size in the DB.

But your table design could use some work: you should not store the same string in multiple rows. Instead you should have a second table which stores the ratings and a foreign key to that table in your Students table. You then use a JOIN to get human readable text when you need it for presentation.

And ... Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

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